开发者

Save UIImage Array to file with UIImagePNGRepresentation

开发者 https://www.devze.com 2023-03-30 09:13 出处:网络
I have an NSArray of UIImage. I wish to save it into disk using UIImagePNGRepresentation. The app show alertview and when clickedButtonAtIndex delegate method occurs I call in background the save met

I have an NSArray of UIImage. I wish to save it into disk using UIImagePNGRepresentation.

The app show alertview and when clickedButtonAtIndex delegate method occurs I call in background the save method:

[self performSelectorInBackground:@selector(saveAll) withObject:nil];

in the saveAll method I make a new autorelease pool and do this work:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString* filename = nil;
int count = [self.imageArray count];
UIImage* image = nil;
NSData* data = nil;
NSData* imageData = nil;

for (int i = 0 ; i < count;) 
{
    image = [self.imageArray objectAtIndex:i];
filename = [[NSString alloc] initWithFormat:@"/saved/%@%d.png",@"aString",i++];

    NSString* completeFilePath = [FileAndBundle getFileDocumentsPath:filename ofType:nil];
    NSLog(@"%d before image saved",i);
    data = [[NSData alloc] initWithData: UIImagePNGRepresentation(image)]开发者_如何学JAVA;
    NSLog(@"%d middle image saved",i);
    imageData= [[NSData alloc] initWithData:[NSData dataWithData:data]];
    NSLog(@"%d after image saved",i);

    [imageData writeToFile:completeFilePath atomically:NO];

    image = nil;
    [data release];
    data = nil;
    [imageData release];
    imageData = nil;
    [filename release];
    filename = nil;

}   
[pool release];

The problem: When I save 12 UIImages the first time all are saved correctly in the disk. When I open again a document with the 12 UIImages and resave it by using the same method In the for loop I get the following error:

ImageIO: PNGNot enough image data

So the image in the disk is partially saved (partial blank image or zero kb image)

and then at random counter value the loop crash by showing this message in the log:

malloc: *** error for object 0x6927804: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug

Can anyone help me in finding the problem?! Thanks!


Dont release data,imageData and filename in for loop. you are releasing variables multiple times.

0

精彩评论

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

关注公众号