【问题标题】:Bad Huffman code?糟糕的霍夫曼代码?
【发布时间】:2011-08-02 17:49:14
【问题描述】:

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

为什么只有我的一些图像损坏了?我需要走一条新的道路并以不同的方式去做吗?

谢谢,
库尔顿

【问题讨论】:

    标签: objective-c xcode ios4 uiimage


    【解决方案1】:

    我觉得这个问题的标题很有趣,我做了一些搜索。 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 世界中广为人知。

    【讨论】:

    • 感谢您的回复。我不认为 SQLite 正在破坏它。它所做的只是存储上传者开始上传的文件位置。您还需要什么其他信息?我很乐意为您提供。谢谢,库尔顿
    • 也可能是您调整大小。取出调整大小的代码,看看会发生什么。
    • 成功上传了大约 10 张图片。所以它必须在resize函数中?
    • 我不确定,但看起来确实如此。研究调整大小的方法,看看你有哪些使用 iOS SDK 进行图像编辑的选项。似乎使用 CG 变换会破坏某些东西。
    • 而不是缩放上下文。尝试使用最终大小初始化它并使用 [UIImage drawInRect:arect]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多