简体   繁体   中英

Scrollview Images creates issue

I am pick Images using Camera in my app & try to show in horizontal scrollview.

But the images are overlapping

My code

-(IBAction)act_photo:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    //picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
     [self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark - Image Picker via Camera
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];

[picker dismissViewControllerAnimated:YES completion:^{
        [arr_images insertObject:chosenImage atIndex:0];
        [self scrollImages_Setup2];

    }];

}

-(void)scrollImages_Setup2
{    
    for (int k=0; k<arr_images.count; k++) {

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((CGRectGetWidth(scl.frame) * k) + CGRectGetWidth(scl.frame), 0, CGRectGetWidth(scl.frame), CGRectGetHeight(scl.frame))];

        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image=[arr_images objectAtIndex:k] ;
        [scl addSubview:imageView];
    }

    scl.contentSize = CGSizeMake((CGRectGetWidth(scl.frame) * arr_images.count)+CGRectGetWidth(scl.frame), CGRectGetHeight(scl.frame));

}

Problem

Image1

在此处输入图片说明

Image 2

在此处输入图片说明

As shown above first I was taken Landscape Image2 & then I was taken Portrait Image 1 .

but as you see, Image 1 has background of Image2.

I think this is because there is something going wrong with scrollview?

Possiblity 2(mostly) Scrollview keeps strong reference of Imageview evenif it is deleted from array

Let me know how to solve this?

Help me to solve this

Thanks

您需要在循环中设置Y值,您的图片Y值随时都可以设置为相同,

Each time you take a new image, your completion-Handler puts the new image as the first element into your array and then calls [self scrollImages_Setup2]; where you iterate over all elements in your array and add them as new subviews to your scrollview.

When you take the first image (Image1) it's getting added to the array and then added as and subview to your scrollview. When you take the second image (Image2) it's getting added to the array as the first element. So then there are two elements in the array (Image2 and Image1) which are both getting added as subview to the scrollview. But Image1 has already been added to the scrollview, so that's causing your problem.

A solution to your problem could be either to always add only the new image and calculate the x position with the number of elements in your array, or to always remove all subviews before you add and layout all images again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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