采用SDWebImage中的实现方法:
+ (UIImage *)decodedImageWithImage:(UIImage *)image {
if (image.images) {
// Do not decode animated images
return image;
}
CGImageRef imageRef = image.CGImage;
CGSize imageSize =CGSizeMake(CGImageGetWidth(imageRef),CGImageGetHeight(imageRef));
CGRect imageRect = (CGRect){.origin =CGPointZero, .size = imageSize};
CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo =CGImageGetBitmapInfo(imageRef);
int infoMask = (bitmapInfo &kCGBitmapAlphaInfoMask);
BOOL anyNonAlpha = (infoMask ==kCGImageAlphaNone ||
infoMask == kCGImageAlphaNoneSkipFirst ||
infoMask == kCGImageAlphaNoneSkipLast);
// CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.
// https://developer.apple.com/library/mac/#qa/qa1037/_index.html
if (infoMask ==kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) >1) {
// Unset the old alpha info.
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
// Set noneSkipFirst.
bitmapInfo |= kCGImageAlphaNoneSkipFirst;
}
// Some PNGs tell us they have alpha but only 3 components. Odd.
elseif (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) ==3) {
// Unset the old alpha info.
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
bitmapInfo |= kCGImageAlphaPremultipliedFirst;
}
// It calculates the bytes-per-row based on the bitsPerComponent and width arguments.
CGContextRef context =CGBitmapContextCreate(NULL,
imageSize.width,
imageSize.height,
CGImageGetBitsPerComponent(imageRef),
0,
colorSpace,
bitmapInfo);
CGColorSpaceRelease(colorSpace);
// If failed, return undecompressed image
if (!context)return image;
CGContextDrawImage(context, imageRect, imageRef);
CGImageRef decompressedImageRef =CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *decompressedImage = [UIImageimageWithCGImage:decompressedImageRefscale:image.scaleorientation:image.imageOrientation];
CGImageRelease(decompressedImageRef);
return decompressedImage;
}
简单说就是:
根据当前输入image的CGImageRef;
根据CGImageRef得到CGSize;
根据CGImageRef生成CGBitmapInfo;
生成一个颜色空间;
根据以上值,生成一个CGBitmap画布,然后draw 一个CGRect大小的CGImageRef;
根据以上画布,得到一个新的CGImageRef;
今天的文章 图片解码方法分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/104252.html