在NSMutable Array中存储来自UIGetScreenImage()的图像

时间:2010-03-30 05:11:50

标签: iphone objective-c

我从UIGetScreenImage()获取图像并直接存储在可变数组中,如: -

image = [UIImage imageWithScreenContents];
[array addObject:image];
[image release];

我已将此代码设置为计时器,因此我无法使用UIImagePNGRepresentation()将其存储为NSData,因为它会降低性能。我想在一段时间之后直接使用这个数组,即在100秒内捕获1000个图像之后。当我使用下面的代码时: -

UIImage *im = [[UIImage alloc] init];
im = [array objectAtIndex:i];
UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);**

应用程序崩溃。 而且我不想在计时器中使用UIImagePNG或JPGRepresentation(),因为它会降低性能。

我的问题是如何使用此数组以便将其转换为图像。 如果有人有相关想法,请与我分享。

2 个答案:

答案 0 :(得分:1)

您不需要在那里的第一个代码示例中释放图像。 [UIImage imageWithScreenContents]返回一个自动释放的对象。

答案 1 :(得分:1)

1. UIImage *im = [[UIImage alloc] init]; 
2. im = [array objectAtIndex:i];
3. UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);

第1行分配并初始化一个新的UIImage对象,该对象在你覆盖第2行的指针后永远不会被释放。你在每次迭代中都会泄漏一个UIImage,你甚至不需要初始化/分配一个新对象。

UIImageWriteToSavedPhotosAlbum([array objectAtIndex:i], nil, nil, nil);

应该工作得很好。

另请注意Carl Norum关于释放自动释放物体的答案。