Adding multiple UIImages for Paging Enabled UIScrollView from ALAssetLibrary

241 views Asked by At

i am having a problem with showing images after fetching from the ALAssetLibrary.My Problem is if there is one image then its coming fine but if there are multiple images then the last image is only appearing but not the prior images.I fount that there is a problem in the Loop and couldn't find if.

Please let me know if anyone resolve this.

mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height)];

mainScrollView.pagingEnabled = YES;

mainScrollView.delegate=self;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;

CGRect innerScrollFrame = mainScrollView.bounds;

for (NSInteger i = 0; i < images.count; i++) {

    imageForZooming=[[UIImageView alloc]init];

    if ([maincheck isEqualToString:@"YES"]) {
        NSURL*url=[NSURL URLWithString:[images objectAtIndex:i]];

        [library assetForURL:url resultBlock:^(ALAsset *asset)
         {
             UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
             [imageForZooming setImage:copyOfOriginalImage];
         }
            failureBlock:^(NSError *error)
         {
             NSLog(@"the error is %@",error);
         }];
    }

    else{
        [imageForZooming setImageWithURL:[NSURL URLWithString:[images objectAtIndex:i]]];
    }

    UITapGestureRecognizer*t=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showblack:)];

    t.numberOfTapsRequired=1;

    imageForZooming.userInteractionEnabled=YES;

    [imageForZooming addGestureRecognizer:t];

    imageForZooming.frame=CGRectMake(0, 50, 320, 200);

    imageForZooming.contentMode=UIViewContentModeScaleAspectFit;

    imageForZooming.tag = VIEW_FOR_ZOOM_TAG;

    pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
    pageScrollView.minimumZoomScale = 1.0f;
    pageScrollView.maximumZoomScale = 4.5f;
    pageScrollView.zoomScale = 1.0f;
    pageScrollView.contentSize = imageForZooming.bounds.size;
    pageScrollView.delegate = self;
    pageScrollView.showsHorizontalScrollIndicator = NO;
    pageScrollView.showsVerticalScrollIndicator = NO;

    [pageScrollView addSubview:imageForZooming];

    [mainScrollView addSubview:pageScrollView];

    if (i < images.count-1) {
        innerScrollFrame.origin.x += innerScrollFrame.size.width;
    }
}

mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width,mainScrollView.bounds.size.height);

[self.view addSubview:mainScrollView];
1

There are 1 answers

7
Dev On

Here you didn't set the content size of the scroll view.

[scrollView setContentSize:CGSizeMake(numberOfImages * scrollView.frame.size.width, scrollView.frame.size.width)];

And also you have to change the X offset (If horizontal scrolling) for each imageView before adding to scrollView.

Also, you didn't add imageForZooming to any scrollView here.

Look here to load the images from ALAssetLibrary.