使用 UIImagePNGRepresentation 将 UIImage 数组保存到文件
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在for循环中释放数据、图像数据和文件名。您多次释放变量。
Dont release data,imageData and filename in for loop. you are releasing variables multiple times.