美文网首页
图片Base64

图片Base64

作者: lanxuping | 来源:发表于2017-03-22 11:33 被阅读13次

    调用方法

    NSString *str  = [self image2DataURL:[UIImage imageNamed:@"guanbi.png"]];
    

    实现方法

    - (BOOL) imageHasAlpha: (UIImage *) image
    {
        CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);
        return (alpha == kCGImageAlphaFirst ||
                alpha == kCGImageAlphaLast ||
                alpha == kCGImageAlphaPremultipliedFirst ||
                alpha == kCGImageAlphaPremultipliedLast);
    }
    - (NSString *) image2DataURL: (UIImage *) image
    {
        NSData *imageData = nil;
        NSString *mimeType = nil;
        
        if ([self imageHasAlpha: image]) {
            imageData = UIImagePNGRepresentation(image);
            mimeType = @"image/png";
        } else {
            imageData = UIImageJPEGRepresentation(image, 1.0f);
            mimeType = @"image/jpeg";
        }
        
        return [NSString stringWithFormat:@"data:%@;base64,%@", mimeType,
                [imageData base64EncodedStringWithOptions: 0]];
        
    }
    

    相关文章

      网友评论

          本文标题:图片Base64

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