这个方法的原理是什么?

读sdwebimage源码看到这个方法,完全不懂
这个方法涉及到的技术在哪可以深入研究?

+ UIImage *+ UIImage *decodedImageWithImage:UIImage *image{ if img.images { return img; } CGImageRef imageRef = img.CGImage; CGSize imgSize = CGSizeMakeCGImageGetWidthimageRef, CGImageGetHeightimageRef; CGRect imgRect = CGRect{.origin = CGPointZero, .size = imgSize}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB; CGBitmapInfo bitmapInfo = CGImageGetBitmapInfoimageRef; int infoMask = bitmapInfo & kCGBitmapAlphaInfoMask; BOOL anyNonAlpha = infoMask == kCGImageAlphaNone || infoMask == kCGImageAlphaNoneSkipFirst || infoMask == kCGImageAlphaNoneSkipLast; // CGBitmapContextCreate 不支持 kCGImageAlphaNone with rgb // https://developer.apple.com/library/mac/#qa/qa1037/_index.html if infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponentscolorSpace > 1 { bitmapInfo &= ~kCGBitmapAlphaInfoMask; bitmapInfo |= kCGImageAlphaNoneSkipFirst; } // Some PNGs tell us they have alpha but only 3 components. Odd. else if !anyNonAlpha && CGColorSpaceGetNumberOfComponentscolorSpace == 3{ bitmapInfo &= ~kCGBitmapAlphaInfoMask; bitmapInfo |= kCGImageAlphaPremultipliedFirst; } CGContextRef context = CGBitmapContextCreateNULL ,imgSize.width ,imgSize.height ,CGImageGetBitsPerComponentimageRef ,0 ,colorSpace ,bitmapInfo; CGColorSpaceReleasecolorSpace; if !context { return img; } CGContextDrawImagecontext, imgRect, imageRef; CGImageRef decompressedImgRef = CGBitmapContextCreateImagecontext; CGContextReleasecontext; UIImage *decompressedImg = [UIImage imageWithCGImage:decompressedImgRef scale:img.scale orientation:img.imageOrientation]; CGImageReleasedecompressedImgRef; return decompressedImg;
}

简单来说。这个方法会强制在非主线程解析图片,加快应用的响应速度。
如果有需要,我可以写一篇文章来详细解释一下,里面的代码逻辑。

发表评论

电子邮件地址不会被公开。 必填项已用*标注