简体   繁体   English

滚动视图图像产生问题

[英]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 图片1

在此处输入图片说明

Image 2 图片2

在此处输入图片说明

As shown above first I was taken Landscape Image2 & then I was taken Portrait Image 1 . 如上所示,首先拍摄了Landscape Image2,然后拍摄了Portrait Image 1。

but as you see, Image 1 has background of Image2. 但是如您所见,图像1具有图像2的背景。

I think this is because there is something going wrong with scrollview? 我认为这是因为scrollview出了点问题?

Possiblity 2(mostly) Scrollview keeps strong reference of Imageview evenif it is deleted from array 可能性2(大多数情况下) Scrollview保留了Imageview的强引用,即使它已从数组中删除

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]; 每次拍摄新图像时,您的完成处理程序会将新图像作为第一个元素放入数组中,然后调用[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. 当您拍摄第一个图像(Image1)时,它将被添加到数组中,然后作为和子视图添加到滚动视图中。 When you take the second image (Image2) it's getting added to the array as the first element. 当您拍摄第二张图片(Image2)时,它会作为第一个元素添加到数组中。 So then there are two elements in the array (Image2 and Image1) which are both getting added as subview to the scrollview. 因此,数组中有两个元素(Image2和Image1)都作为子视图添加到滚动视图中。 But Image1 has already been added to the scrollview, so that's causing your problem. 但是Image1已经被添加到滚动视图中,因此导致了您的问题。

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. 解决该问题的方法可能是始终只添加新图像并使用数组中元素的数量计算x位置,或者总是在再次添加和布局所有图像之前删除所有子视图。

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

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