圖片邊加載變顯示
根據對SDWebimage的研究,在SDWebImageDownloaderOperator中有邊加載變顯示的代碼,根據自己的研究,簡化了以下的例子
#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;
}
// 模擬圖片的部分的不完整數據
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