加载不完整图片达到边加载边显示

图片边加载变显示

根据对SDWebimage的研究,在SDWebImageDownloaderOperator中有边加载变显示的代码,根据自己的研究,简化了以下的例子

#import 
@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;
    }

// 模拟图片的部分的不完整数据
    NSData *subData = [data subdataWithRange:NSMakeRange(0,self.size)];
    if (self.size > data.length) {
        return;
    }
    
    [self showPartioalImageData:subData];
    
    
}

// 将不完整的图片数据创建为完整图片的尺寸显示,主要包括的内容为以下几部分
1、通过数据获取完整图片的长、宽、方向信息
2、将部分图片以完整图片的尺寸画在画布上,没有的地方用存色填补
3、将画布上的内容还原成图片并显示
使用以上几步就能做到边下载边显示的过程

- (void)showPartioalImageData:(NSData *)partialData
{
    // 1、通过数据获取完整图片的长、宽、方向信息
    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、将画布上的内容还原成图片并显示
        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

你可能感兴趣的:(加载不完整图片达到边加载边显示)