代码之家  ›  专栏  ›  技术社区  ›  sagarkothari

uiimage-是否应加载[uiimage imagenamed:@”“]?

  •  0
  • sagarkothari  · 技术社区  · 15 年前
    • 我的应用程序中有许多图像。(图像超过50张-大约可以根据客户需要扩展)
    • 每个图像都是非常大的圆形,大约为-1024x 768&150 dpi
    • 现在,我必须在滚动视图中添加所有这些图像并将其显示出来。

    好的,我的问题如下。

    据我所知,有两种加载大图像的方法

    1. 图像名:@“
    2. 调用viewdidload时异步加载。

    哪个更好?


    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;
    }
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   zoul    15 年前

    你知不知道这些图像不可能一下子被记住?如果你想在它们之间滚动,你必须把它们页进页出,以保持一个像样的内存占用。

    至于加载它们,您可能应该显示一个微调器,开始在后台加载图像,然后在图像准备好后将其替换为图像。使用加载图像 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
    }
    

    方法名是从内存中拼写的,我可能遗漏了一个参数。但原则上,这个密码是正确的。

        2
  •  0
  •   Kevin Sylvestre    15 年前

    异步加载几乎总是首选以防止用户界面无响应(特别是在viewdidload中)。

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