1

I am doing some testing in my iPhone app and I get this error:

<Error>: Corrupt JPEG data: bad Huffman code

I have seen some weird errors, but this one is really weird. I am also getting some other corrupt errors from it to that I can't remember. Let me post my code to write these files in order.

Step 1: Take a picture then save it

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

Step 2: Start the uploads and possibly resize it:

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

And here's the function used to scale the image:

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

Why are ONLY some of my images getting corrupt? Would I need to take a new path and do it a different way?

Thanks,
Coulton

iosfreak
  • 5,228
  • 11
  • 59
  • 102

1 Answers1

8

I thought the title of this question interesting and I did some searching. Wikipedia explains that Huffman coding is:

an entropy encoding algorithm used for lossless data compression

Your JPG data is getting corrupted somewhere and the iOS SDK is reporting it. As for how and why, Google has a number of results.

According to this post, it seems that MySQL may be corrupting your images. Of course, your images can be corrupted in a few places, since you are processing them. It would take more information to diagnose the exact issue.

Edit:

It seems as though your resizing function is causing the issue. Try comparing your code to the methods mentioned in this thread and see if they will help you at all. As mentioned there by Matthew Frederick, Matt Gemmell's MGImageUtilities may be the what you need. (Although I've never used or even see them.) Matt Gemmell is known in the Mac/Objective-C world.

Community
  • 1
  • 1
Moshe
  • 57,511
  • 78
  • 272
  • 425
  • Thanks for your reply. I don't think SQLite is corrupting it. All it does is store the file location for the uploader to start uploading. What other information do you need? I would be glad to provide it for you. Thanks, Coulton – iosfreak Apr 11 '11 at 02:50
  • It could also be your resizing. Take out the resizing code and see what happens. – Moshe Apr 11 '11 at 02:52
  • Uploaded around 10 images successfully. So it has to be in the resize function? – iosfreak Apr 11 '11 at 03:08
  • I am not certain, but it would seem so. Research your resizing method and see what options you have with the iOS SDK for image editing. Seems like using CG transforms is breaking something. – Moshe Apr 11 '11 at 03:10
  • Instead of Scaling the context . Try init'ing it with final size and using [UIImage drawInRect:arect] – Warren Burton Apr 11 '11 at 09:44