漸進(jìn)渲染大圖對(duì)加載大圖特別重要,地圖都是局部加載,放大移動(dòng)時(shí)候一部分一部分的漸進(jìn)的加載,一般在對(duì)大圖進(jìn)行網(wǎng)絡(luò)請(qǐng)求時(shí)候,可以獲得一部分?jǐn)?shù)據(jù)就加載一部分?jǐn)?shù)據(jù)。
用本地圖片和定時(shí)器來(lái)模擬網(wǎng)絡(luò)加載圖片。
@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)];//得到新增字節(jié)然后渲染圖片。
[_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++;
}