(转)把两个Layer融合成一个UIImage的截图方法
//layer1 CCLayer *layer1 = [CCLayer node]; CGPoint p1 = layer1.anchorPoint; [layer1 setAnchorPoint:ccp(0,0)]; CCRenderTexture *renderer01 = [CCRenderTexture renderTextureWithWidth: layer1.contentSize.width height: layer1.contentSize.height]; [renderer01 begin]; [layer1 visit]; [renderer01 end]; [layer1 setAnchorPoint: p1]; UIImage *image1 = [UIImage imageWithData:[renderer01 getUIImageAsDataFromBuffer:kCCImageFormatPNG]]; //layer2 CCLayer *layer2 = [CCLayer node]; CGPoint p2 = layer2.anchorPoint; [layer2 setAnchorPoint:ccp(0,0)]; CCRenderTexture *renderer02 = [CCRenderTexture renderTextureWithWidth: layer2.contentSize.width height: layer2.contentSize.height]; [renderer02 begin]; [layer2 visit]; [renderer02 end]; [layer2 setAnchorPoint: p2]; UIImage *image2 = [UIImage imageWithData:[renderer02 getUIImageAsDataFromBuffer:kCCImageFormatPNG]]; //GC来了两个层开始融合 UIImage *image; if (UIGraphicsBeginImageContextWithOptions != NULL) { UIGraphicsBeginImageContextWithOptions(srcSize, YES, 0); } else { UIGraphicsBeginImageContext(srcSize); } [image1 drawInRect:CGRectMake(0, 0, srcSize.width, srcSize.height)]; [image2 drawInRect:CGRectMake(0, 0, srcSize.width, srcSize.height)]; image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();