简体   繁体   English

NSMutableArray为图像创建问题

[英]NSMutableArray creates issue for images

I have an array with dictionaries in it. 我有一个带有字典的数组。 Each dictionary contains UIImage & String values. 每个字典都包含UIImage和String值。

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]; 因此,我使用了[_postObj.arr_images addObject:dict]; instead of insertObject 代替insertObject

Help me to solve this 帮我解决这个问题

Objects use reference counting and both NSMutableArray and UIImageView will retain the UIImage object. 对象使用引用计数, NSMutableArrayUIImageView都将保留 UIImage对象。

This means that removing it from the array will not automatically remove it from the UIImageView and you must remove it from both explicitly: 这意味着将其从数组中删除不会自动将其从UIImageView删除,并且您必须同时将其从两个对象中明确删除:

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

Problem is in your frame setting of UIImageView . 问题出在您的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. 基本上,我正在根据先前图像的宽度调整图像的X位置。

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]; [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. 请删除scrollview contentSize,因为在数组中添加多个图像时image_array会增加,并且size宽度会很大。 so remove arr_images.count as well. 因此也要删除arr_images.count。

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

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