populating images in UIScrollView. And how to do i

2019-09-08 17:02发布

Well this is my usual domain now, I have managed some functionality, but I need to make it better.

I am trying to populate recent images from camera roll. I'm pretty sure there is more elegant way to do it. Another thing thats making me uneasy is, this is

Block > loop > another block Any better solution or simplification is appreciated.

  ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                 if (nil != group) {

                                     [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                                     NSLog(@"%d images found", group.numberOfAssets);
                                     for(int i = group.numberOfAssets - 5; i<group.numberOfAssets - 1; i++){

                                     [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i]
                                                             options:0
                                                          usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                                                              if (nil != result) {
                                                                  ALAssetRepresentation *repr = [result defaultRepresentation];

                                                                  UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]];

                                                                  CGFloat aspectRatio = img.size.width/img.size.height;
                                                                  UIImageView *imgView = [[UIImageView alloc] init];

                                                                  imgView.frame = CGRectMake(10, self.yCord, 300, 300 /aspectRatio);

                                                                  self.yCord += margin + (300/aspectRatio);

                                                                  imgView.image = img;

                                                                  imgView.layer.shadowRadius = 5.0;
                                                                  imgView.layer.shadowColor = [UIColor blackColor].CGColor;

                                                                  [self.scroll addSubview:imgView];

                                                                  *stop = YES;
                                                                  self.scrollViewHeight = self.yCord + imgView.frame.size.height + margin;
                                                                  CGSize scrollViewSize = CGSizeMake(320, self.scrollViewHeight);
                                                                  [self.scroll setContentSize:scrollViewSize];

                                                              }
                                                          }];}
                                 }

                                 *stop = NO;
                             } failureBlock:^(NSError *error) {
                                 NSLog(@"error: %@", error);
                             }];

Also how do I make images do lazy load. I have paging disabled for now, and all the images load simultaneously, which is hazardous in most cases.

1条回答
神经病院院长
2楼-- · 2019-09-08 17:36

Actually you should use NSOperation and NSOperationQueueClass for this purpose or can use GCD for the same too. But if you look into the Guide Reference for corresponding Classes you find NSOperation and NSOperation Classes has High level Flexibility as comparability to GCD. Here I am going to give you some reference of tutorial at where you can get the idea how to use these mention Useful Class for Such Purpose efficiently.

Here is the link for NSOperation in this tutorial writer mention evreything in much detail, i am pretty sure it will really helps you if you look into NSOperation and NsoperationQueue use in doing same task i.e downloading images or fetching images from camera in background efficiently.

Here is the tutorial for the GCD.

Here I'll like to tell you, why i have given you these tutorial references because these tutorial gives a lot of help in implementing such thing very efficiently.

Hey Friend try to get some point form Above, here I have introduced my experience only.It's up to you how you takes my reference.

I Feel happy if you get any idea from above.

EDIT: Friend Don't Forget about the most important part of your requirement that could be minimum memory occupation by your Classes (UIScrollView). Think suppose there are so many images in Camera and you really need to fetch all images might be in hundreds of in terms of numbers suppose you going to show that images over the UIImageView. Here You need to do Follow the concept of Infinite ScrollView(creates only three mainView just refresh the data over these mainView ). i.e Create only Three mainView view1(this would be treat as right view), view2(would be treat as center) and View3(seems as left). You need to write code in such way you need only these view to allocate and add all UIImageViews over these three Views.You need to use Delegate of UIScrollView for managing all that things. Here i have given just logic only and you have to go through over it and even i am forcing you that you should follow this thing in your app, but for sake for sake of high speed app you should think over it. You can many of thread over the internet for the same logic and could find many tutorials for this Logic. And please don't try to blame me in case of if you don't able to do same, here i given my opinion for better development.

I hope you understand my views.

查看更多
登录 后发表回答