坏哈夫曼代码?

时间:2011-04-11 02:38:42

标签: objective-c xcode ios4 uiimage

我正在我的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;
}

为什么我的一些图片只会腐败?我是否需要采取新的方式并采取不同的方式?

谢谢,
库尔顿

1 个答案:

答案 0 :(得分:4)

我认为这个问题的标题很有趣,我做了一些搜索。 Wikipedia explains that Huffman coding is

  

用于无损数据压缩的熵编码算法

你的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世界中很有名。