简体   繁体   English

在iPhone上下载asynchoronus图像

[英]download asynchoronus images on iphone

how to make this code asynchronous?? 如何使此代码异步?

as this code allow to change image on tap in imageview but right now its slow 因为此代码允许在imageview中点击更改图像,但现在它的速度很慢

- (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];
}

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. 这些请求支持取消(当图像视图移出屏幕时),并且全部通过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). 为了使其无缝,需要进行大量的线程工作(阅读:不是针对noob的),但是它对于具有许多图像的大型表非常有用(因为对于可容纳在其上的物理内存的图像数量而言,天花板非常低)内存最少的iOS设备)。

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. 如果没有,那么您可以使用performSelectorInBackground:…来研究实现,然后让对象执行自己的锁定/处理。 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::follow-up: thanks but where should i declare this method?? 问::跟进:谢谢,但是我应该在哪里声明此方法? inside if (sections == 3) { } – user437503 内部if(sections == 3){} – user437503

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];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM