加載不完整圖片達到邊加載邊顯示

圖片邊加載變顯示

根據(jù)對SDWebimage的研究,在SDWebImageDownloaderOperator中有邊加載變顯示的代碼,根據(jù)自己的研究,簡化了以下的例子

#import <ImageIO/ImageIO.h>
@interface ViewController ()
@property (nonatomic,strong) UIImageView * imageView;
@property (nonatomic,assign) NSInteger count;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *img = [UIImage imageNamed:@"banner1.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];
    imageView.x = 20;
    imageView.width = imageView.width/2;
    imageView.height = imageView.height/2;
    imageView.y = 200;
    self.imageView = imageView;
    
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkShow)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)displayLinkShow
{
    self.count +=1;
    if (self.count %10 == 0) {
        self.count = 0;
    }else{
        return;
    }
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"banner1.jpg" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:NULL];
    self.size += data.length/60;
    if (self.size>=data.length) {
        self.size = data.length;
    }

// 模擬圖片的部分的不完整數(shù)據(jù)
    NSData *subData = [data subdataWithRange:NSMakeRange(0,self.size)];
    if (self.size > data.length) {
        return;
    }
    
    [self showPartioalImageData:subData];
    
    
}

// 將不完整的圖片數(shù)據(jù)創(chuàng)建為完整圖片的尺寸顯示,主要包括的內(nèi)容為以下幾部分
1、通過數(shù)據(jù)獲取完整圖片的長、寬、方向信息
2、將部分圖片以完整圖片的尺寸畫在畫布上,沒有的地方用存色填補
3、將畫布上的內(nèi)容還原成圖片并顯示
使用以上幾步就能做到邊下載邊顯示的過程

- (void)showPartioalImageData:(NSData *)partialData
{
    // 1、通過數(shù)據(jù)獲取完整圖片的長、寬、方向信息
    CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)partialData, NULL);
    
    size_t width = 0;
    size_t height = 0;
    UIImageOrientation orientation = UIImageOrientationUp;
    
    if (width + height == 0) {
        CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0,NULL);
        if (properties) {
            NSInteger orientationValue = -1;
            CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
            if (val) CFNumberGetValue(val, kCFNumberLongType, &height);
            val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
            if (val) CFNumberGetValue(val, kCFNumberLongType, &width);
            val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
            if (val) CFNumberGetValue(val, kCFNumberNSIntegerType, &orientationValue);
            CFRelease(properties);
            orientation = [self orientationFromPropertyValue:orientationValue];
            
        };
    }
    
    // 2、將部分圖片以完整圖片的尺寸畫在畫布上,沒有的地方用存色填補
    if (width+height>0) {
        
        
        CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        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, CGRectMake(0, 0, width, partialHeight), partialImageRef);
                CGImageRelease(partialImageRef);
                partialImageRef = CGBitmapContextCreateImage(bmContext);
                CGContextRelease(bmContext);
            }else{
                CGImageRelease(partialImageRef);
                partialImageRef = nil;
            }
        }
        // 3、將畫布上的內(nèi)容還原成圖片并顯示
        if (partialImageRef) {
            UIImage *image = [UIImage imageWithCGImage:partialImageRef scale:1 orientation:orientation];
            CGImageRelease(partialImageRef);
            self.imageView.image = image;
        }
    }
}
- (UIImageOrientation)orientationFromPropertyValue:(NSInteger)value {
    switch (value) {
        case 1:
            return UIImageOrientationUp;
        case 3:
            return UIImageOrientationDown;
        case 8:
            return UIImageOrientationLeft;
        case 6:
            return UIImageOrientationRight;
        case 2:
            return UIImageOrientationUpMirrored;
        case 4:
            return UIImageOrientationDownMirrored;
        case 5:
            return UIImageOrientationLeftMirrored;
        case 7:
            return UIImageOrientationRightMirrored;
        default:
            return UIImageOrientationUp;
    }
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容