恼人的错误:损坏的JPEG数据:数据段的过早结束

时间:2011-04-11 23:43:44

标签: objective-c xcode ios4

这是我在向Apple发送应用程序之前的最后一条错误消息(希望如此),这让我发疯了。将图像保存到手机时,会破坏文件。当它试图打开文件时,它会给我错误Corrupt JPEG data: premature end of data segment并崩溃。

这是保存图片的代码:

- (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结束......就像那样。

感谢
库尔顿

1 个答案:

答案 0 :(得分:2)

您的代码假设一切顺利,但您应该检查一些返回值和错误。特别是,在iOS上,通常不允许您写入主目录。所以文件可能根本没有保存。

您应该使用documents目录,并验证文件是否正确写入。 writeToFile:调用返回BOOL表示成功。