iOS image转base64

作者: fordG | 来源:发表于2019-04-02 10:12 被阅读1次
    - (NSString *) convertImage: (UIImage *) image
    {
        NSData *imageData = nil;
        NSString *mimeType = nil;
        
        if ([self imageHasAlpha: image]) {
            imageData = UIImagePNGRepresentation(image);
            mimeType =  pngType;
        } else {
            imageData = UIImageJPEGRepresentation(image, 1.0f);
            mimeType = jpgType;
        }
        
        return [NSString stringWithFormat: base64, mimeType,
                [imageData base64EncodedStringWithOptions: 0]];
        
    }
    
    - (BOOL) imageHasAlpha: (UIImage *) image
    {
        CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
        return (alpha == kCGImageAlphaFirst ||
                alpha == kCGImageAlphaLast ||
                alpha == kCGImageAlphaPremultipliedFirst ||
                alpha == kCGImageAlphaPremultipliedLast);
    }
    
    • tip 服务端接收数据是, android和ios可能转换的不一样, 服务端如果不做处理需要, 此处代码是weex代码, js分离获取base64字符串


      image.png

    相关文章

      网友评论

        本文标题:iOS image转base64

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