Catch ImageIO:<错误> </error>

时间:2012-03-13 16:17:18

标签: iphone objective-c xcode javax.imageio catch-block

是否可以捕获错误“ImageIO:JPEGMaximum支持的图像尺寸为65500像素”

我已经为相应的方法编写了try,catch部分。但是catch块没有捕获到ImageIO异常!!

以下代码catch块永远不会捕获它。对此的任何帮助都是赞赏的。

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


}

1 个答案:

答案 0 :(得分:3)

该方法不会抛出异常,因此无需捕获。如果要接收错误消息,则应将回调作为第三个参数传递:

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

然后您可以实现以下方法,一旦保存图像将调用该方法:

- (void)           image: (UIImage *) image
didFinishSavingWithError: (NSError *) error
             contextInfo: (void *) contextInfo;