Why did the SDWebImage redraw imageSource in the connection:didReceiveData: method?

只愿长相守 提交于 2019-12-11 15:55:10

问题


look at the code snippet below. i don't understand the code between #ifdef TARGET_OS_IPHONE and #endif,should i redraw a new image from the imageSource? why?

 - (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
// ...
}

(the method is in SDWebImageDownloaderOperation.m, the version is SDWebImage 3.7.0). anyone can explain it, thanks!

来源:https://stackoverflow.com/questions/49271260/why-did-the-sdwebimage-redraw-imagesource-in-the-connectiondidreceivedata-meth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!