首页 > 代码库 > Base64图片与UIImage的相互转化
Base64图片与UIImage的相互转化
http://blog.csdn.net/fudesign2008/article/details/38903085
Base64图片 -> UIImage
[objc] view plain copy
- - (UIImage *) dataURL2Image: (NSString *) imgSrc
- {
- NSURL *url = [NSURL URLWithString: imgSrc];
- NSData *data = [NSData dataWithContentsOfURL: url];
- UIImage *image = [UIImage imageWithData: data];
- return image;
- }
UIImage -> Base64图片
[objc] view plain copy
- - (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图片与UIImage的相互转化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。