iOS Base64图片与UIImage的相互转化

释放双眼,带上耳机,听听看~!
  1. Base64图片 -> UIImage

    - (UIImage *) dataURL2Image: (NSString *) imgSrc
    
    {
    
        NSURL *url = [NSURL
    URLWithString: imgSrc];
    
        NSData *data = [NSData
    dataWithContentsOfURL: url];
    
        UIImage *image = [UIImage
    imageWithData: data];
    
        return image;
    
    } 

     

UIImage -> Base64图片

- (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 [NSStringstringWithFormat:@"data:%@;base64,%@", mimeType,

            [imageData base64EncodedStringWithOptions:
0]];

}

  //头像做base64处理

        NSData *originData =
UIImagePNGRepresentation(img.image);

        NSData *encodeData = [GTMBase64
encodeData:originData];

        NSString *encodeResult = [[NSStringalloc] initWithData:encodeData
encoding:NSUTF8StringEncoding];

  //头像做base64处理

 

    NSData *originData =
UIImagePNGRepresentation(img.image);

        NSData *encodeData = [GTMBase64
encodeData:originData];

        NSString *encodeResult = [[NSStringalloc] initWithData:encodeData
encoding:NSUTF8StringEncoding];


 

人已赞赏
iOS文章

iOS runtime 运行时机制

2020-4-30 14:16:57

iOS文章

iOS UIAlertController

2020-4-30 15:31:48

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索