iPhone 临时文件写入中的竞争条件 (?)

发布于 2024-09-11 00:05:08 字数 723 浏览 5 评论 0原文

我正在 iPad 模拟器中创建一些临时文件。为了测试我的文件创建,我创建了该文件,然后读回它。下面是一些代码来说明这一点:

-(NSString *) writeToTempFile:(UIImage*) image{
NSString *path = [self createTemporaryFile];
NSLog(@"path: %@", path);
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES]; 
free(data);
return path;
}

-(UIImage *) readTempFile:(NSString *) path{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:data];
return image;
}

在最终函数将 UIImage 写入相册之前,我依次调用这些方法。

UIImageWriteToSavedPhotosAlbum(image2, self, nil, nil);

问题是,这总是在我的应用程序第三次执行时崩溃。第一次和第二次它成功地完成了所有这些并存储到专辑中。第三次坠毁回家。有什么想法吗?

I'm creating some temporary files in the iPad simulator. To test my file creation, I create the file and then read it back. Here's some code to show this:

-(NSString *) writeToTempFile:(UIImage*) image{
NSString *path = [self createTemporaryFile];
NSLog(@"path: %@", path);
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES]; 
free(data);
return path;
}

-(UIImage *) readTempFile:(NSString *) path{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:data];
return image;
}

I call these methods one after another, before a final function writes out the UIImage to the photo album.

UIImageWriteToSavedPhotosAlbum(image2, self, nil, nil);

The problem is, this always crashes my app on the third time it is executed. First and second time it successfully does all of this and stores to the album. Third time it crashes to Home. Any ideas?

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

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

发布评论

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

评论(1

尹雨沫 2024-09-18 00:05:08
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES]; 
free(data);

从 UIImageJPEGRepresentation 返回的 NSData 是 -autoreleased。不需要free()它。 free() 任何 Objective-C 对象都是错误——而是发送 -release 消息。

请通读内存管理编程指南< /em>

NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES]; 
free(data);

The NSData returned from UIImageJPEGRepresentation is -autoreleased. There is no need to free() it. And it is wrong to free() any Objective-C objects — send a -release message instead.

Please read through the Memory Management Programming Guide.

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