Catch ImageIO: <ERROR> - PullRequest
       11

Catch ImageIO: <ERROR>

0 голосов
/ 13 марта 2012

возможно ли поймать ошибку "ImageIO: JPEGMaximum поддерживает размер изображения 65500 пикселей"

Я написал попытку, раздел catch для соответствующего метода. Однако блок catch не сделалне поймать исключение ImageIO !!

Приведенный ниже блок перехвата кода никогда его не перехватит.Любая помощь по этому вопросу приветствуется.

-(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 Ответ

3 голосов
/ 13 марта 2012

метод не выдает исключение, поэтому ловить нечего.если вы хотите получить сообщение об ошибке, вы должны передать обратный вызов в качестве третьего аргумента:

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

, тогда вы можете реализовать следующий метод, который будет вызываться после сохранения изображения:

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