简体   繁体   English

如何懒惰地从照片库加载图像?

[英]How to Load images from photo library lazily?

I would like to load images lazily and displaying it in scrollview in which paging is enabled. 我想延迟加载图像并在启用分页的scrollview中显示它。 Loading all images in a single shot leads App to crash, as i am loading fullScreenImage. 一次加载所有图像会导致App崩溃,因为我正在加载fullScreenImage。 Its working fine, If I load thumbnail image using aspectRatioThumbnail, but image quality is worse. 如果我使用AspectRatioThumbnail加载缩略图,它的效果很好,但是图像质量较差。 So I want to load images for only visible pages instead of loading everything at a time. 因此,我只想为可见页面加载图像,而不是一次加载所有内容。 If I know how to load images lazily from photo library, I can display images using only three pages. 如果我知道如何从照片库延迟加载图像,则只能使用三页显示图像。 below are my code... 以下是我的代码...

-(void)loadImagesFromLibrary{

     imageList = [[NSMutableArray alloc] init];
     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

     CGFloat __block xaxis=05;
     NSUInteger __block images = 0;

     dispatch_queue_t queue = dispatch_get_main_queue();
     dispatch_async(queue, ^(void){

     [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                           usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                               if(group){

                                   [group setAssetsFilter:[ALAssetsFilter allPhotos]];

                                   [group enumerateAssetsUsingBlock:^(ALAsset *assets, NSUInteger index, BOOL *innerStop){

                                       if (assets && [[assets valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
                                       {
                                           //ALAssetRepresentation *representation = [assets defaultRepresentation ];
                                           //UIImage *img = [UIImage imageWithCGImage:[representation fullScreenImage]];

                                           UIImage *img = [UIImage imageWithCGImage:[assets aspectRatioThumbnail]];
                                           if(img){

                                               [imageList addObject:img];

                                               UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xaxis, 5, 300, HEIGHT)];
                                               [imageView setImage:[self rectSizeFormat:img Size:CGSizeMake(300, HEIGHT)]];      //resizing the image 
                                               [imageView setTag:images++];
                                               [imageView setUserInteractionEnabled:YES];

                                               UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(displayImageInMiddleView:)];
                                               [tapGesture setNumberOfTapsRequired:1];
                                               [imageView addGestureRecognizer:tapGesture];
                                               [tapGesture release];

                                               [scrollView addSubview: imageView];
                                               [imageView release];

                                               xaxis+=320;

                                           }  
                                       }
                                   }];

                                  [scrollView setContentSize:CGSizeMake(xaxis, HEIGHT)];
                               }
                               else{

                                   DLog(@"No image");
                               }

                           }

                         failureBlock:^(NSError *err) {

                             DLog(@"%@", [err localizedDescription]);
                         }];

    });
}

I know it can't be explained in few lines. 我知道无法用几行来解释。 All I am asking is give me a hint to achieve what I want, or suggest me how I can do it in a different way. 我要问的只是给我一个提示,以实现我想要的目标,或者建议我如何以其他方式实现目标。 Many thanks in advance. 提前谢谢了。

I have a similar issue with a "filmstrip" type display. 我对“幻灯片”类型的显示器有类似的问题。 My solution is to enumerate the assets in my target album and for each one 我的解决方案是枚举目标相册中的资产,并逐一列出

  • Create a userImage object and add it to an NSMutableArray. 创建一个userImage对象,并将其添加到NSMutableArray。
  • Save the assetURL in the object. 将assetURL保存在对象中。
  • Get the aspect ratio thumbnail and save it in the object. 获取长宽比缩略图并将其保存在对象中。
  • Put the thumbnail image into the filmstrip. 将缩略图图像放入幻灯片中。

When I need a higher-resolution version of each filmstrip image I fire off an async request to load the full-sized image using the saved assetURL. 当我需要每个胶片图像的高分辨率版本时,我会触发一个异步请求,以使用保存的assetURL加载完整尺寸的图像。 Of course that is asynchronous so you have to have a place in the UI (UIImageView in my case) to put the image when it finally arrives from the asset library. 当然这是异步的,因此,当图像最终从资产库到达时,您必须在UI中放置一个位置(在我的情况下为UIImageView)以放置图像。 Before the full-sized image is loaded I display the thumbnail in the same UIImageView (with scale-aspect-fit). 在加载完整尺寸的图像之前,我将缩略图显示在同一UIImageView中(具有scale-aspect-fit)。

The asynchronous nature of the image loads is a real annoyance. 图像加载的异步特性是一个真正的烦恼。

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

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