我做在我的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;
}
为什么只有一些图像变坏?我需要采取一条新路,以不同的方式去做吗?
感谢,
库尔顿
感谢您的答复。我不认为SQLite正在破坏它。它所做的只是存储上传器的文件位置以开始上传。你还需要什么其他信息?我很乐意为您提供。谢谢,Coulton – iosfreak 2011-04-11 02:50:52
它也可能是你的调整大小。取出调整大小的代码,看看会发生什么。 – Moshe 2011-04-11 02:52:12
好的 - 我会试试。 – iosfreak 2011-04-11 02:58:38