opengl es 绘图图像 保存为图片。读取并显示图片,添加在layer上

来源:互联网 发布:由诲女知之乎的读音 编辑:程序博客网 时间:2024/04/30 03:12

背景色为透明时也适用。。。见到过一种保存方式,如果opengl透明度打开时,背景色设为透明而保存的是黑色。。

这个方法则完全ok。

//保存成图片- (UIImage*)cropImage{    NSInteger myDataLength = self.bounds.size.width*self.bounds.size.height*4 ;GLubyte * buffer = (GLubyte*) malloc(myDataLength);    memset(buffer, 0, myDataLength);glReadPixels(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,                                                              buffer,                                                              myDataLength,                                                              NULL);CGImageRef iref = CGImageCreate(self.bounds.size.width,self.bounds.size.height,8,32,self.bounds.size.width*4,CGColorSpaceCreateDeviceRGB(),kCGImageAlphaPremultipliedLast,provider,NULL,NO,kCGRenderingIntentDefault);size_t wi         = CGImageGetWidth(iref);size_t he        = CGImageGetHeight(iref);UIGraphicsBeginImageContext(CGSizeMake(wi, he));CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, wi, he), iref);UIImage* outputImage =  UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext(); CGDataProviderRelease(provider);CGImageRelease(iref); free(buffer);buffer = NULL;return outputImage;}
UIImage *image = [UIImage imageWithContentsOfFile:                  [readingContents.readingUserDataPaintingPath stringByAppendingPathComponent:[[NSString alloc] initWithFormat:@"%@%d.png",@"undoPicture", readingContents.readingPageno]]];    if(image != nil){        //创建层        CALayer *lay = [CALayer layer];        //    self.backgroundColor = [UIColor redColor].CGColor;//设置背景色        lay.bounds = CGRectMake(0, 0, image.size.width,image.size.height);//层设置为图片大小        lay.contents =(id)image.CGImage;        lay.position = CGPointMake(320 / 2.0 , 480 / 2.0);//层在view的位置        //        lay.position = CGPointMake(768 / 2.0 , 1024 / 2.0);//层在view的位置        [self.view.layer addSublayer:lay];//将层加到当前View的默认layer下    }
原创粉丝点击