将帧保存到iOS中的图像时,“收到内存警告”

时间:2014-01-24 16:37:05

标签: ios ipad opengl-es uiimage

下面的代码是将电影的每一帧保存到iPad设备的相册中...大约60帧保存到图像中...它给了我'收到内存警告..'错误,我的应用程序最终崩溃..什么这里出错了?我错过了什么......?我使用的是iOS 7 ......

  -(UIImage *) getImageFromGLBuffer : GLuint frameBuffer : (CGCize) screenSize
{
    glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer);

    int width = screenSize.width;
    int height = screenSize.height;

    NSInteger iDataLength =  width * height * 4;

    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc( iDataLength );
    glReadPixels(0,  0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    glBindFramebuffer( GL_FRAMEBUFFER, 0);

    // 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(iDataLength);

    for(int y = 0; y <height; y++)
    {
        for(int x = 0; x < width * 4; x++)
        {
            buffer2[(int)((height - 1 - y) * width * 4 + x)] = buffer[(int)(y * 4 * width + x)];
        }
    }

    // Release the first buffer
    free((void*)buffer);

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

    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * width;

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

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

    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);

    // then make the UIImage from that
    UIImage *image =  [UIImage imageWithCGImage:imageRef]; //[[UIImage alloc] initWithCGImage:imageRef];

    NSData* imageData =  UIImagePNGRepresentation(image);     // get png representation
    UIImage* pngImage = [UIImage imageWithData:imageData];

    CGImageRelease(imageRef);

    return pngImage;
}

// callback for CGDataProviderCreateWithData
void releaseBufferData(void *info, const void *data, size_t dataSize)
{
    NSLog(@"releaseBufferData\n");
    // free the buffer
    free((void*)data);
}

- (void)saveImageToPhotoAlbum : GLuint frameBuffer : (CGCize) screenSize
{
    if( iqFrameBuffer != nil )
    {
        UIImage* image = [self getImageFromGLBuffer:frameBuffer : screenSize];

        if( image != nil)
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            });
        }
        else
        {
            NSLog(@"Couldn't save to Photo Album due to invalid image..");
        }
    }
    else
    {
        NSLog(@"Frame buffer is invalid. Couldn't save to image..");
    }
}

// callback for UIImageWriteToSavedPhotosAlbum
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSLog(@"Image has been saved to Photo Album successfully..\n");
 //   [image release];   // release image
    image = nil;
}

0 个答案:

没有答案