views:

251

answers:

1

I've not found a answer to this question anywhere, but this seems like a typical problem: I am taking pictures from an iPhone 4, and I have to send these pics to a server through a POST-Request, but the server does not accept pictures that are bigger than 0,5 MB, so I have to compress the pictures before I send them. In order to achieve this, I am calling following method: "NSData *imageData = UIImageJPEGRepresentation(tempImage, 0.7);", which means: I am compriming the image data, and it takes a lot of time (about 10s / pic).

Is there any way to control the quality of the camera device in order to take low quality pictures?

Thanks in advance for helping.

A: 

Anything that will block the main thread should be done in a background thread, including converting an image to a JPEG. So you should start the JPEG conversion using performSelectorInBackground:withObject: and, when the conversion is done, pass the resulting NSData object back to the main thread.

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissModalViewControllerAnimated:YES];
    [self performSelectorInBackground:@selector(encodePhotoInBackground:) withObject:image];
}

- (void)encodePhotoInBackground:(UIImage*)image {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.60);

    [self performSelectorOnMainThread:@selector(saveImageData:) withObject:imageData waitUntilDone:NO];

    [pool release];
}

- (void)saveImageData:(NSData*)imageData {
    // Do something with the image
}
Michael Nachbaur