加载图像UICollectionView异步? [英] Load image to UICollectionView Asynchronously?

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

问题描述

我如何可以加载图像到 UICollectionview 异步?
里面下面的方法?

How can i load images to a UICollectionview asynchronously? Inside following method?

- (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

 bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[preview objectAtIndex:indexPath.row] lastPathComponent]]];
 [[cell grid_image] setImage:bookImage];

}

viewDidLoad中()我用下面的非同步调用来加载图像preVIEW的NSMutableArray

In viewdidload() i am using following asynch call to load images to "preview "NSMutablearray

 dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.GMM.assamkar", NULL);

dispatch_async(imageLoadQueue, ^{
    //Wait for 5 seconds...
    usleep(1000000);
    docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


    for(int k=0; k <[allImage count] ;k++){


        imgURL = [allImage objectAtIndex:k];
        [imagePreview addObject:imgURL];
        imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];


        [imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [allImage lastPathComponent]] atomically:YES];

    }

    [[self collectionView] reloadData];


});

请帮忙me..Now其花费过多时间用于加载...

Please help me..Now its taking too much time for loading...

推荐答案

尝试回答你的主要问题:我怎样才能加载图像到 UICollectionview 异步?

Trying to answer your main question "How can i load images to a UICollectionview asynchronously?"

我建议你通过提供的解决方案的娜塔莎Murashev 的<一个href=\"http://natashatherobot.com/ios-how-to-download-images-asynchronously-make-uitableview-scroll-fast/\">here,这很好地工作对我来说,这很简单。

I would suggest solution offered by "Natasha Murashev" here, which worked nicely for me and it's simple.

如果这里 imgUrl的= [allImage objectAtIndex:K]; allImage 属性你保持URL的数组,然后更新你的的CollectionView:cellForItemAtIndexPath:的方法是这样的:

If here imgURL = [allImage objectAtIndex:k]; in allImage property you keep array of URLs, then update your collectionView:cellForItemAtIndexPath: method like this:

- (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *url = [NSURL URLWithString:[allImage objectAtIndex:indexPath]];

    [self downloadImageWithURL:url completionBlock:^(BOOL succeeded, NSData *data) {
        if (succeeded) {
            cell.grid_image.image = [[UIImage alloc] initWithData:data];
        }
    }];
}

和添加方法 downloadImageWithURL:completionBlock:你的课,当图像被成功下载将自动异步加载的图像和更新细胞的CollectionView。

And add method downloadImageWithURL:completionBlock: to your class, which will load images asynchronously and update cells in CollectionView automatically when images are successfully downloaded.

- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, NSData *data))completionBlock
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (!error) {
            completionBlock(YES, data);
        } else {
            completionBlock(NO, nil);
        }
    }];
}

我看你尝试preLOAD图像前视图出现,所以也许我的解决方案是不是你的东西,但是从你的问题就很难说了。任何怎么样,你可以达到你想要有什么用为好。

I see you try to preload images before view appears so maybe my solution isn't what you what, but from your question it's hard to say. Any how, you can achieve what you want with this as well.

让我知道这是任何好转。

Let me know if this is any better..

这篇关于加载图像UICollectionView异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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