使用 UIImagePNGRepresentation 将 UIImage 数组保存到文件

发布于 2024-12-01 05:41:03 字数 1646 浏览 3 评论 0原文

我有一个 UIImage 的 NSArray。 我希望使用 UIImagePNGRepresentation 将其保存到磁盘中。

应用程序显示alertview,当 clickedButtonAtIndex 委托方法发生时,我在后台调用 save 方法:

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

在 saveAll 方法中,我创建一个新的自动释放池并执行此工作:

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

问题: 当我第一次保存 12 个 UIImages 时,所有图像都正确保存在磁盘中。 当我再次打开包含 12 个 UIImage 的文档并使用相同的方法重新保存它时 在 for 循环中我收到以下错误:

ImageIO:PNG图像数据不足

因此磁盘中的图像被部分保存(部分空白图像或零 kb 图像)

,然后在随机计数器值处循环崩溃,并在日志中显示以下消息:

malloc:*** 对象 0x6927804 错误:已释放对象的校验和不正确 - 对象可能在释放后被修改。 *** 在malloc_error_break中设置断点进行调试

谁能帮我找到问题所在?!谢谢!

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)];
    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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2024-12-08 05:41:03

不要在for循环中释放数据、图像数据和文件名。您多次释放变量。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文