首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImage -是否应该加载[UIImage imageNamed:@“”?

UIImage -是否应该加载[UIImage imageNamed:@“”?
EN

Stack Overflow用户
提问于 2010-04-10 06:52:28
回答 2查看 1.9K关注 0票数 0
  • 我的应用程序中有很多图像。(图像超过50 -大约-它可以根据客户的需要扩展)
  • 每幅图像都很大,大约-1024x768&150个dpi
  • 现在,我必须将所有这些图像添加到一个滚动视图中并显示出来。

好的,我的问题如下。

据我说,有两个选项可以加载大图像,

  1. imageNamed:@"“
  2. 在viewDidLoad调用.

时异步加载

哪一个更好?

代码语言:javascript
复制
imgModel.image=[UIImage imageNamed:[dMain valueForKey:@"imgVal"]];

或者像这样。

代码语言:javascript
复制
    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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-04-10 07:15:06

你知不知道这些图像不能同时进入记忆?如果您想在它们之间滚动,则必须将它们分页进出,以保持良好的内存占用。

至于加载它们,您可能应该显示一个旋转器,开始在背景中加载图像,然后在图像准备好后用它替换它。使用NSURLConnection加载映像太过分了,使用imageNamed:可能会更容易

代码语言:javascript
复制
- (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
}

方法名是从内存中拼写出来的,我可能在这里或那里漏掉了一个对词。但原则上,代码是正确的。

票数 3
EN

Stack Overflow用户

发布于 2010-04-10 07:05:17

异步加载几乎总是用于防止没有响应的用户界面(特别是在viewDidLoad中)。

此外,如果您有大量的图像(而且它们是大型文件),您可能只想加载将是可见的图像(加上一些缓冲区),并在滚动到这些图像时加载它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2612546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档