2011-08-25 73 views
1

我有一個NSArray的UIImage。 我希望使用UIImagePNGRepresentation將其保存到磁盤中。保存UIImage數組與UIImagePNGRepresentation文件

這個應用程序顯示alertview當clickedButtonAtIndex委託方法時我在後臺調用保存方法:

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

在白水方法我提出一個新的自動釋放池和做好這項工作:

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個UIImages文檔並通過使用相同的方法 在重新保存for循環我得到以下錯誤:

ImageIO: PNGNot enough image data

所以在磁盤的圖像被部分地保存(局部的空白圖像或零KB圖像)

,然後隨機計數器的值通過顯示在日誌中此消息循環崩潰:

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

誰能幫我找到這個問題?謝謝!

+0

這個問題似乎是通過使用不同的文件名來解決的。 如果PNG文件在磁盤存在,然後嘗試使用問題它可以給錯誤的訪問和寫入相同源的方法來覆蓋它(我假設的UIImage從磁盤獲取數據也..)) 所以用另一個名字保存png,然後將其移至原始名稱。 – Giuseppe

回答

0

不要在for循環中釋放數據,imageData和文件名。你多次釋放變量。