简体   繁体   中英

NSMutableArray creates issue for images

I have an array with dictionaries in it. Each dictionary contains UIImage & String values.

But when I try to delete object using

[arr removeObjectAtIndex:button.tag];

then it just decreases the count but image (Object) is not deleted. So, my Images are overlapping when try to delete.

It also creates problem when I try to add objects in the array

[arr insertObject:dict atIndex:0];

so, I used [_postObj.arr_images addObject:dict]; instead of insertObject

Help me to solve this

Objects use reference counting and both NSMutableArray and UIImageView will retain the UIImage object.

This means that removing it from the array will not automatically remove it from the UIImageView and you must remove it from both explicitly:

[arr removeObjectAtIndex:button.tag];
_imageView.image = nil;

Problem is in your frame setting of UIImageView . Please try using below code (not tested by myself) and see if this fixes the issue for you. Basically, I am adjusting X position of images based on previous image's width.

CGFloat imageXM = 5.0;
CGFloat imageXPos = 0;

for (UIImage *image in arr_images) {
    CGRect imageFrame = CGRectMake(imageXPos + imageXM, 0.0, image.size.width, image.size.height);
    imageXPos = imageXPos + image.size.width;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];

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

As in your code

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

    }];

[arr_images insertObject:chosenImage atIndex:0]; that means you need only one image to display on scroll view. Then why you take a loop:

-(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));

}

You just right the code only for display the Image. you don't need to scroll view for displaying only one image. I think you are not clear what you want.

Your problem regarding the overwriting the images because of loop. so remove the loop, when you display a single image. Please remove the scrollview contentSize, because image_array will increase, when you add the multiple images in array and size width will be large. so remove arr_images.count as well.

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