在iPhone上下载asynchoronus图像 [英] download asynchoronus images on iphone

查看:39
本文介绍了在iPhone上下载asynchoronus图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使此代码异步?

由于此代码允许在imageview中点击更改图像,但现在速度很慢

as this code allow to change image on tap in imageview but right now its slow

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger sections = [indexPath section]; 

    if (sections  == 3)
    { 
        ltxt.text = [NSString stringWithFormat:@" %d of %d", tap,[a1 count]];


        if(tap<[a1 count]-1) {

            tap++;

    NSString *sa=[a1 objectAtIndex:tap];

            image= [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat: sa,[a1 objectAtIndex:tap ]]]]];


          //  NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]; 


            myImageView.image = image;
            //[myimg release];
}

推荐答案

这取决于您在任何给定时间需要请求多少张图像.我发现用于工业强度设计/请求的一个很好的解决方案是创建一个请求漏斗(减少了图像数量).这些请求支持取消(当图像视图离开屏幕时),并且全部通过NSOperationQueue处理.为了使其无缝,需要进行大量的线程工作(阅读:不是针对noob的),但是它对于具有许多图像的大表非常有用(因为对于可容纳在其上的物理内存的图像数量而言,天花板太低了)内存最少的iOS设备.

it depends on how many images you'll need to request at any given time. a good solution i found for industrial strength design/requests was to create a request funnel (which reduced the number of images). these requests supported cancellation (for when the image view goes off-screen), and were all handled via an NSOperationQueue. there was a ton of threading work (read: not for the noob) to get it seamless, but it worked great for large tables which had many images (since the ceiling is quite low for the number of images you can fit into physical memory on the iOS device with the least amount of memory).

这是一种正确"的方法,如果您要下载大量图像.如果没有,那么您可以使用performSelectorInBackground:…来研究实现,然后让对象执行自己的锁定/处理.无论哪种方式,您都必须在下载图像时执行某些操作/显示某些内容,这样在接收到图像时不会阻塞调用线程(通常是主线程).

that's one 'right' way, if you have a lot of images to download. if not, then you can look into implementation using performSelectorInBackground:…, and just let the object perform its own locking/handling. either way, you'll have to do something/display something while the image is being downloaded, so the calling thread (typically the main thread) is not blocked while the image is received.

Q ::跟进:谢谢,但是我应该在哪里声明这种方法呢?内部if(sections == 3){} – user437503

Q::follow-up: thanks but where should i declare this method?? inside if (sections == 3) { } – user437503

A ::它将采用一般"形式:

A::it will take the `general' form:

- (void)setCellImage:(UIImage *)img {
/* assert here if called from secondary thread */
    myImageView.image = img;
}

- (void)udpateImageFollowingTap {
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    NSString * imageUrl = [self.a1 objectAtIndex:self.tap];
    UIImage * tmpImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
    [self performSelectorOnMainThread:@selector(setCellImage:) withObject:tmpImage];
    [tmpImage release];
    [pool release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger sections = [indexPath section];
    if (sections == 3) {
        ltxt.text = [NSString stringWithFormat:@" %d of %d", tap,[a1 count]];
        if(tap<[a1 count]-1) {
            tap++;
            /* insert code to discard previous image and display loading image indication here */
            [self performSelectorInBackground:@selector(udpateImageFollowingTap) withObject:0];
        }
    }
}

这篇关于在iPhone上下载asynchoronus图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆