美文网首页程序员iOS DeveloperiOS 开发
swift -- oc UIimage 和 NSString

swift -- oc UIimage 和 NSString

作者: iOSTbag | 来源:发表于2016-06-17 14:25 被阅读826次

    swift

        let image = UIImage.init(named: "1024-01")
                /// t图片转data
        let data = UIImagePNGRepresentation(image!)
        // data 转string 注意是base64EncodedStringWithOptions 这个方法不能使用         [NSString.init(data: <#T##NSData#>, encoding: <#T##UInt#>)] 必须是这个编码
    
        let str = data?.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
    
        // string转data
        
        let data1 = NSData(base64EncodedString:str!,options: .IgnoreUnknownCharacters)
        // data 转 图片
        let image2 = UIImage.init(data: data1!)
        
        print(image2)
    

    oc

    UIImage *image = [UIImage imageNamed:@"640"];
    NSData *data = UIImagePNGRepresentation(image);
    NSString *str = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    NSLog(@"%@",str);
    
    
    NSData *data1 = [[NSData alloc] initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
    UIImage *image1 = [[UIImage alloc] initWithData:data1];
    NSLog(@"%@",image1);

    相关文章

      网友评论

        本文标题:swift -- oc UIimage 和 NSString

        本文链接:https://www.haomeiwen.com/subject/mmdvdttx.html