好的,我的问题如下。
据我说,有两个选项可以加载大图像,
时异步加载
哪一个更好?
imgModel.image=[UIImage imageNamed:[dMain valueForKey:@"imgVal"]];或者像这样。
NSURL *ur=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:lblModelName.text ofType:@"png"] isDirectory:NO];
NSURLRequest *req=[NSURLRequest requestWithURL:ur cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:40];
[ur release];
NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
if(con){
myWebData=[[NSMutableData data] retain];
} else {
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[myWebData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[myWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"ImageView Ref From - %@",imgV);
// my image view & set image
imgV.image=[UIImage imageWithData:myWebData];
[connection release]; connection=nil;
}发布于 2010-04-10 07:15:06
你知不知道这些图像不能同时进入记忆?如果您想在它们之间滚动,则必须将它们分页进出,以保持良好的内存占用。
至于加载它们,您可能应该显示一个旋转器,开始在背景中加载图像,然后在图像准备好后用它替换它。使用NSURLConnection加载映像太过分了,使用imageNamed:可能会更容易
- (void) startLoadingImage {
// You need an autorelease pool since you are running
// in a different thread now.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageNamed:@"foo"];
// All GUI updates have to be done from the main thread.
// We wait for the call to finish so that the pool won’t
// claim the image before imageDidFinishLoading: finishes.
[self performSelectorOnMainThread:@selector(imageDidFinishLoading:)
withObject:image waitUntilDone:YES];
[pool drain];
}
- (void) viewDidLoad {
UIActivityIndicator *spinner = …;
[self.view addSubview:spinner];
[self performSelectorInBackground:@selector(startLoadingImage)
withObject:nil];
}
- (void) imageDidFinishLoading: (UIImage*) image {
// fade spinner out
// create UIImageView and fade in
}方法名是从内存中拼写出来的,我可能在这里或那里漏掉了一个对词。但原则上,代码是正确的。
发布于 2010-04-10 07:05:17
异步加载几乎总是用于防止没有响应的用户界面(特别是在viewDidLoad中)。
此外,如果您有大量的图像(而且它们是大型文件),您可能只想加载将是可见的图像(加上一些缓冲区),并在滚动到这些图像时加载它们。
https://stackoverflow.com/questions/2612546
复制相似问题