iOS-渐进渲染大图

渐进渲染大图对加载大图特别重要,地图都是局部加载,放大移动时候一部分一部分的渐进的加载,一般在对大图进行网络请求时候,可以获得一部分数据就加载一部分数据。

用本地图片和定时器来模拟网络加载图片。

@interfaceViewController(){

NSMutableData* _data;

NSData* _allData;

NSUInteger length;

UIImageView* _imageView;

NSTimer* timer;

NSInteger le;

}

@end

@implementation ViewController

-(void)viewDidload{

[super viewDidload];

_data = [NSMutableData alloc]init];

NSString *path =[[NSBundlemainBundle]pathForResource:@"Portrait-ns@2x"ofType:@"png"];

_allData = [NSDatadataWithContentsOfFile:path];

length = _allData.length;

le = length/10;

timer = [NSTimer scheduledTimerWithTimeInterval:1target:self selector:@selector(updateImage) userInfo:nil repeats:YES];

_imageView = [[UIImageViewalloc]initWithFrame:self.view.frame];

[self.view addSubview:_imageView];

}

}

-(void)updateImage{

staticintindex =0;

if(index==10) {

return;

}

NSUInteger l;

if(index==9) {

l=length-le*9;

}else{

l= le;

}

Byte  by[l];

[_allData getBytes:by range:NSMakeRange(index*le, l)];//得到新增字节然后渲染图片。

[_data appendBytes:by length:l];

CGImageSourceRefmyImageSource =CGImageSourceCreateWithData((CFDataRef)_data,NULL);

CGImageRefmyImage =CGImageSourceCreateImageAtIndex(myImageSource,0,NULL);

CFRelease(myImageSource);

_imageView.image = [UIImageimageWithCGImage:myImage];

//    image.image = [UIImage imageNamed:@"image.ico"];

index++;

}

你可能感兴趣的:(iOS-渐进渲染大图)