2011-04-11 817 views
0

這是我將應用程序提交給蘋果之前的最後一條錯誤消息(希望),這讓我發瘋。將圖像保存到手機時,會損壞文件。當它試圖打開文件時,它給我錯誤Corrupt JPEG data: premature end of data segment和崩潰。有令人討厭的錯誤:損壞的JPEG數據:數據段過早結束

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo { 
[picker.parentViewController dismissModalViewControllerAnimated:NO]; 

uploadImage = image; 
int orient = uploadImage.imageOrientation; 
NSString *theOrientation = [NSString stringWithFormat: @"%d", orient]; 

NSString *latestIDQuery = @""; 
NSArray *results = [database executeQuery:@"SELECT * FROM processes ORDER BY id DESC LIMIT 0,1"]; 
for (NSDictionary *row in results) { 
    latestIDQuery = [row valueForKey:@"id"]; 
} 

int latestID = [latestIDQuery intValue]; 

int newID = latestID + 1; 

NSString *newIDString = [[NSString alloc] initWithFormat:@"%d", newID]; 
NSString *imageURL = [NSString stringWithFormat:@"Documents/%@_process.jpg",newIDString]; 

NSLog(@"Saving here... %@", imageURL); 

NSString *uploadImagePath = [NSString stringWithFormat:@"%@_process.jpg",newIDString]; 

NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:imageURL]; 
NSLog(@"Needs to write something like this: %@", jpgPath); 
[UIImageJPEGRepresentation(uploadImage, 1.0) writeToFile:jpgPath atomically:YES]; 

[database executeNonQuery:@"INSERT INTO processes (image, album, status, orient, ready) VALUES (?, ?, ' In Queue', ?, 'no');", uploadImagePath, selectedID, theOrientation]; 

TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate]; 
dataCeter.dataSix = nil; 
NSString *databaseURL = [NSString stringWithFormat:@"%@_process.jpg",newIDString]; 
dataCeter.dataSix = databaseURL; 

[self showCaption]; 

} 

是保存圖像,使他們不會得到腐敗的一個更好的方法:

這裏是我的代碼保存圖像?另外,你如何檢查一個圖像是否腐敗,所以你永遠不會再崩潰?我聽說imageData應該以FF d8開頭,並以d9 ff結尾..類似的東西。

感謝
庫爾頓

回答

2

你的代碼假設一切順利,但你應該檢查些返回值和錯誤。特別是,在iOS上,您通常不允許寫入主目錄。所以文件可能根本沒有保存。

您應該使用documents目錄,並驗證文件是否寫入沒有錯誤。調用返回一個表示成功的BOOL。

+0

文件寫在那裏。 – iosfreak 2011-04-12 00:16:59

+0

對不起,試圖破譯你的法語。畢竟,我看到你正在寫文檔目錄。所以這些文件實際上不會寫在那裏。我仍然建議檢查返回值和錯誤。祝你好運。 – mvds 2011-04-12 00:33:27

相關問題