2011-04-11 203 views
1

我做在我的iPhone應用程序的一些測試,我得到這個錯誤:壞霍夫曼碼?

<Error>: Corrupt JPEG data: bad Huffman code 

我已經看到了一些奇怪的錯誤,但是這是一個非常奇怪的。我也從其中得到一些其他的腐敗錯誤,我不記得了。讓我發佈我的代碼以便按順序編寫這些文件。

第1步:拍照然後保存

- (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]; 

NSLog(@"Check it: 1"); 

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]; 
} 

第2步:開始上傳,並有可能調整其大小:

 NSString *sqlImageUploadPathOne = @"./../Documents/"; 
     NSString *sqlImageUploadPathTwo = [rowtwo valueForKey:@"image"]; 
     NSString *getCaption = [rowtwo valueForKey:@"caption"]; 
     NSString *getTheID = [rowtwo valueForKey:@"id"]; 
     NSString *getOrientation = [rowtwo valueForKey:@"orient"]; 

     NSString *jpgPath = [NSString stringWithFormat:@"Documents/%@",sqlImageUploadPathTwo]; 
     NSString *jpgPathTwo = [NSString stringWithFormat:@"./../Documents/%@",sqlImageUploadPathTwo]; 
     NSString *yourPath = [NSHomeDirectory() stringByAppendingPathComponent:jpgPath]; 

     // Resize and Save 
     UIImage *tempImageTwo = [UIImage imageNamed:jpgPathTwo]; 
     float tooBig = 800.0; 
     UIImage *tempImage = [self scaleImage:tempImageTwo:tooBig:tooBig]; 
     NSData *imageDataTwo = [NSData dataWithData:UIImageJPEGRepresentation(tempImage, 0.1)]; 
     [imageDataTwo writeToFile:jpgPathTwo atomically:YES]; 

下面是使用縮放功能圖片:

- (UIImage *)scaleImage:(UIImage *) image: (float)maxWidth:(float) maxHeight 

{

CGImageRef imgRef = image.CGImage; 

CGFloat width = CGImageGetWidth(imgRef); 
CGFloat height = CGImageGetHeight(imgRef); 

if (width <= maxWidth && height <= maxHeight) 
{ 
    return image; 
} 

CGAffineTransform transform = CGAffineTransformIdentity; 
CGRect bounds = CGRectMake(0, 0, width, height); 

if (width > maxWidth || height > maxHeight) 
{ 
    CGFloat ratio = width/height; 
    if (ratio > 1) 
    { 
     bounds.size.width = maxWidth; 
     bounds.size.height = bounds.size.width/ratio; 
    } 
    else 
    { 
     bounds.size.height = maxHeight; 
     bounds.size.width = bounds.size.height * ratio; 
    } 
} 
CGFloat scaleRatio = bounds.size.width/width; 
UIGraphicsBeginImageContext(bounds.size); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
CGContextTranslateCTM(context, 0, -height); 
CGContextConcatCTM(context, transform); 
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return imageCopy; 
} 

爲什麼只有一些圖像變壞?我需要採取一條新路,以不同的方式去做嗎?

感謝,
庫爾頓

回答

4

我覺得這個問題有意思的標題,我做了一些搜索。 Wikipedia explains that Huffman coding is

an entropy encoding algorithm used for lossless data compression

您的JPG數據在某處被損壞,iOS SDK正在報告它。至於如何和爲什麼,Google has a number of results

According to this post,似乎MySQL可能被破壞你的形象。當然,由於您正在處理它們,因此您的圖像可能會在幾個地方被損壞。這需要更多的信息來診斷確切的問題。

編輯:

它好像你的尺寸調整功能造成問題。嘗試比較你的代碼在this thread提到的方法,看看他們是否會幫助你的。正如Matthew Frederick有提到,Matt Gemmell's MGImageUtilities可能是你所需要的。 (雖然我從來沒有用過,甚至看不到它們。)Matt Gemmell在Mac/Objective-C世界中是衆所周知的。

+0

感謝您的答覆。我不認爲SQLite正在破壞它。它所做的只是存儲上傳器的文件位置以開始上傳。你還需要什麼其他信息?我很樂意爲您提供。謝謝,Coulton – iosfreak 2011-04-11 02:50:52

+0

它也可能是你的調整大小。取出調整大小的代碼,看看會發生什麼。 – Moshe 2011-04-11 02:52:12

+0

好的 - 我會試試。 – iosfreak 2011-04-11 02:58:38