Question

is it possible to catch the error "ImageIO: JPEGMaximum supported image dimension is 65500 pixels"

I have wrote try,catch section for the corresponding method.However the catch block didn't catch the ImageIO exception!!

The below code catch block will never catch it. any help on this is Appreciated.

-(void)captureToPhotoAlbum {

    @try {       

        UIImage *image = [self glToUIImage]; //to get an image

        if (image != nil) {

            UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
            [self eraseView];
            [self showAnAlert:@"Saved Successfully" Message:@""];

        }
        else {

            [self showAnAlert:@"Can't Save!!" Message:@"An error occurred "];
        }



    }

    @catch (NSException *ex) {

        [self showAnAlert:@"Can't Save!!" Message:@"An error occurred"];
    }
}



-(UIImage *) glToUIImage {

    UIImage *myImage = nil;

    @try {


        NSInteger myDataLength = 800 * 960 * 4;

        // allocate array and read pixels into it.
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, 800, 960, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        // gl renders "upside down" so swap top to bottom into new array.
        // there's gotta be a better way, but this works.
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);

        for(int y = 0; y < 960; y++)
        {
            for(int x = 0; x < 800 * 4; x++)
            {
                buffer2[(959 - y) * 800 * 4 + x] = buffer[y * 4 * 800 + x];
            }
        }

        // make data provider with data.
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

        // prep the ingredients
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * 800;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

        // make the cgimage
        CGImageRef imageRef = CGImageCreate(800, 444444444, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

        // then make the uiimage from that
        myImage = [UIImage imageWithCGImage:imageRef];
    }
    @catch (NSException *ex) {

        @throw;
    }
    @finally {

        return myImage;
    }


}
Was it helpful?

Solution

the method does not throw an exception, so there is nothing to catch. if you want to receive an error message, you should pass a callback as the third argument:

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

then you can implement the following method, which will be called once the image is saved:

- (void)           image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
             contextInfo: (void *) contextInfo;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top