Search code examples
iphoneiosios4uiimagepickercontroller

Capturing photos with specific resolution using the UIImagePickerController


I would like to take photos of A4 pieces of paper with writing on. Importantly, I want the text to be readable, but I do not want images with resolutions like 2592x1936 pixel or 3264x2448 pixel as that would be too big. Also, I assume that rescaling the photo after capturing takes extra time, so I would like to avoid this too.

We can choose between the following qualities:

UIImagePickerControllerQualityTypeHigh = 0   
UIImagePickerControllerQualityTypeMedium          = 1  default value   
UIImagePickerControllerQualityTypeLow            = 2   
UIImagePickerControllerQualityType640x480         = 3,   
UIImagePickerControllerQualityTypeIFrame1280x720  = 4,   
UIImagePickerControllerQualityTypeIFrame960x540   = 5 

If we were using the AVFoundation, we could choose resolutions from this nice table (under headline "Capturing Still Images").

But is there a similar table for UIImagePickerController, which for example says that UIImagePickerControllerQualityTypeHigh equals 1920x1080 on iPhone 3gs?


Solution

  • UIImagepickerController quality is used for video recording (used in UIImagepickerController property "videoQuality").

    I guess that if you want to specify what the exact photo resolution should be, you should use the AV Foundation framework instead of the UIImagepickerController. Or as you said, convert the picture afterwards.

    To resize it afterwards (found here):

    //  ==============================================================
    //  resizedImage
    //  ==============================================================
    // Return a scaled down copy of the image.  
    
    UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
    {
        CGImageRef          imageRef = [inImage CGImage];
        CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);
    
        // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
        // see Supported Pixel Formats in the Quartz 2D Programming Guide
        // Creating a Bitmap Graphics Context section
        // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
        // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
        // The images on input here are likely to be png or jpeg files
        if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
        // Build a bitmap context that's the size of the thumbRect
        CGContextRef bitmap = CGBitmapContextCreate(
                    NULL,
                    thumbRect.size.width,       // width
                    thumbRect.size.height,      // height
                    CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                    4 * thumbRect.size.width,   // rowbytes
                    CGImageGetColorSpace(imageRef),
                    alphaInfo
            );
    
        // Draw into the context, this scales the image
        CGContextDrawImage(bitmap, thumbRect, imageRef);
    
        // Get an image from the context and a UIImage
        CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
        UIImage*    result = [UIImage imageWithCGImage:ref];
    
        CGContextRelease(bitmap);   // ok if NULL
        CGImageRelease(ref);
    
        return result;
    }
    

    Hope this helps!