开发者

Image not stored in an NSMutableArray

i am trying to store image in NsMutable Array.But it not working in that function. But eventually in other function it work properly.can someone tell me why it is not working.

- (void)readPlistData {

objectButtonArray = [[NSMutableArray alloc] init];   \\Use to store Buttons
editButtonArray = [[NSMutableArray alloc] init];      \\ another array use to store deletebutton(such as.crossmark button.)
selectImage = [[NSMutableArray alloc] init];

self.myPlistPath = [NSString stringWithString:[self plistPath]];

plistArray = [[NSMutableArray alloc] initWithContentsOfFile:self.myPlistPath];

for (int i =0; i< [plistArray count];i++)
{


    UIGraphicsEndImageContext();

    NSData *imageData = [plistArray objectAtIndex:i];
    currentObjectImage = [UIImage imageWithData:imageData] ; 
    [selectImage addObject:currentObjectImage]; \\value not stored in selectimage NSMutableArray

    CGRect imageSize = CGRectMake(0, 0, 100, 90);

    UIGraphicsBeginImageContext(imageSize.size); // this will crop

    [currentObjectImage drawInRect:imageSize];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    imageButton = [[UIButton alloc]initWithFrame:CGRectMake(width, 0, 100, 90)];
    [imageButton setTag:tag];
    [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [imageButton setImage:newImage forState:UIControlStateNormal];

    CGRect backFrame = backView.frame;
    backFrame.size.width = 120+width;
    backView.frame = backFrame;

    [backView addSubview:imageButton];

    editButton = [[UIButton alloc]initWithFrame:CGRectMake(width-10, -10, 35, 35)];
    [editButton setImage:[UIImage imageNamed:@"DeleteButton.png"] forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
    editButton.hidden = YES;
    [editButton setTag:tag];
    [backView addSubview:editButton];
    tag++;
    width = 120 +开发者_如何学Python width;

    [objectButtonArray addObject:imageButton];
    [editButtonArray addObject:editButton];

}

scrollView.contentSize = backView.bounds.size;  

}

can someone help me.


I think the problem is Reference name "currentObjectImage" basically when you are doing

[selectImage addObject:currentObjectImage];

it saves the reference of "currentObjectImage", where in the next iteration next image is loaded in it which creates problem.

Replace above line with

[selectImage addObject:[currentObjectImage copy]];

I think it will solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜