4

iPhone で複雑なペイント アプリケーションを開発しようとしています。私は現在、Quartzを使用して描画しています(例CGContext...)。残念ながら、Quartz のオーバーヘッドは、私が行っている種類の描画には遅すぎるため、GLPaint の例を参照点として使用して OpenGL 呼び出しに移植しています。

EAGLview クラス (Quartz に相当) から UIImage/CGImage を取得する方法はあり UIGraphicsGetImageFromCurrentImageContextますか? 基本的にはGLPaintアプリで描いた絵を保存する必要があります。

4

5 に答える 5

15
-(UIImage *) saveImageFromGLView
{
    NSInteger myDataLength = 320 * 480 * 4;
    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, 320, 480, 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 <480; y++)
    {
        for(int x = 0; x <320 * 4; x++)
        {
            buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + 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 * 320;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    // make the cgimage
    CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}
于 2009-12-22T11:09:23.803 に答える
7

@Quakeboyの答えと同じですが、サイズを動的に決定できるようにビューを渡します(これをユニバーサルアプリに使用しました):

- (UIImage *)saveImageFromGLView:(UIView *)glView {
    int width = glView.frame.size.width;
    int height = glView.frame.size.height;

    NSInteger myDataLength = width * height * 4;
    // allocate array and read pixels into it.
    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, width, height, 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 < height; y++)
    {
        for(int x = 0; x < width * 4; x++)
        {
            buffer2[((height - 1) - y) * width * 4 + x] = buffer[y * 4 * width + 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 * 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);
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    return myImage;
}
于 2011-01-26T16:54:09.213 に答える
3

それは間違いなく可能です。トリックは、glReadPixels を使用して、画像データを OpenGL フレームバッファから使用可能なメモリにプルすることです。画像データへのポインターを取得したら、CGDataProviderCreateWithData と CGImageCreate を使用して、データから CGImage を作成できます。私は、この手法を多用する OpenGL ベースの描画アプリに取り組んでいます!

于 2009-06-04T00:26:50.693 に答える
0

このコードは、上記のソリューションのようにメモリをリークすることはなく、動的なビュー サイズと Retina と標準ディスプレイを考慮しています。

-(BOOL)iPhoneRetina{
    return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?YES:NO;
}

void releasePixels(void *info, const void *data, size_t size) {
    free((void*)data);
}

-(UIImage *) glToUIImage{

    int imageWidth, imageHeight;

    int scale = [self iPhoneRetina]?2:1;

    imageWidth = self.frame.size.width*scale;
    imageHeight = self.frame.size.height*scale;

    NSInteger myDataLength = imageWidth * imageHeight * 4;

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

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

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

    // make the cgimage

    CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    UIImage *myImage = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationDownMirrored]; //Render image flipped, since OpenGL's data is mirrored

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);

    CGDataProviderRelease(provider);

    return myImage;
}

CGDataProviderCreateWithData への最後のパラメーターはメモリを解放する関数であると想定されているため、他の関数はメモリ リークを起こし、CGRelease 関数も省略しています。

于 2014-01-30T11:11:41.010 に答える
-1
void SaveScreenImage()

{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGImageRef cgImage = UIGetScreenImage();
void *imageBytes = NULL;
if (cgImage == NULL) {
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
imageBytes = malloc(320 * 480 * 4);
CGContextRef context = CGBitmapContextCreate(imageBytes, 320, 480, 8, 320 * 4, colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorspace);
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        CGRect bounds = [window bounds];
        CALayer *layer = [window layer];
        CGContextSaveGState(context);
        if ([layer contentsAreFlipped]) {
            CGContextTranslateCTM(context, 0.0f, bounds.size.height);
            CGContextScaleCTM(context, 1.0f, -1.0f);
        }
[layer renderInContext:(CGContextRef)context];
        CGContextRestoreGState(context);
    }
    cgImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
}
UIImage *image=[UIImage imageWithCGImage:cgImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 
[pool release];
}

このコードは、画面に表示されているように保存されますが、プライベート API である可能性があります。

于 2010-07-04T09:06:17.337 に答える