为什么SDWebImage在连接中重绘了imageSource:didReceiveData:method?

时间:2018-03-14 06:44:42

标签: ios uiimage core-graphics sdwebimage

查看下面的代码段。我不理解#ifdef TARGET_OS_IPHONE和#endif之间的代码,我应该从imageSource重新绘制一个新图像吗?为什么呢?

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.imageData appendData:data];

if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0 && self.completedBlock) {

    // Get the total bytes downloaded
    const NSInteger totalSize = self.imageData.length;

    // Update the data source, we must pass ALL the data, not just the new bytes
    CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)self.imageData, NULL);
    // ...
    if (width + height > 0 && totalSize < self.expectedSize) {
        // Create the image
        CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);

// here is my question.
#ifdef TARGET_OS_IPHONE
        // Workaround for iOS anamorphic image
        if (partialImageRef) {
            const size_t partialHeight = CGImageGetHeight(partialImageRef);
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
            CGColorSpaceRelease(colorSpace);
            if (bmContext) {
                CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = partialHeight}, partialImageRef);
                CGImageRelease(partialImageRef);
                partialImageRef = CGBitmapContextCreateImage(bmContext);
                CGContextRelease(bmContext);
            }
            else {
                CGImageRelease(partialImageRef);
                partialImageRef = nil;
            }
        }
#endif
// ...
}

(方法在SDWebImageDownloaderOperation.m中,版本为SDWebImage 3.7.0)。 任何人都可以解释一下,谢谢!

0 个答案:

没有答案