更快速地从URL加载图像并将其显示在iPhone应用程序中

Anj*_*ulu 3 iphone imageview ios

我使用以下代码在我的ImageView中显示图像:

   imgbackBG.image = [UIImage imageWithData:
                   [NSData dataWithContentsOfURL:
                    [NSURL URLWithString:[NSString stringWithFormat:@"http://%@",    [test.arrImagessplash objectAtIndex:[test.arrImages count]-4]]]]];

    4cing.com/mobile_app/uploads/pageicon/splash.png
Run Code Online (Sandbox Code Playgroud)

问题是代码执行速度非常慢.有没有办法加载图像并更快地在ImageView中显示它?如果是这样,我该怎么做?

Ram*_*eti 18

您正在使用的代码在主线程上加载图像.这将阻止UI.使用GCD异步加载图像.

以下是示例代码:

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(q, ^{
            /* Fetch the image from the server... */
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *img = [[UIImage alloc] initWithData:data];
            dispatch_async(dispatch_get_main_queue(), ^{
                /* This is the main thread again, where we set the tableView's image to
                 be what we just fetched. */
                cell.imgview.image = img;
            });
        });
Run Code Online (Sandbox Code Playgroud)