ios 从 CIImage 获取 CGImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14402413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a CGImage from CIImage
提问by tagyro
I have a UIImage which is loaded from a CIImage with:
我有一个从 CIImage 加载的 UIImage :
tempImage = [UIImage imageWithCIImage:ciImage];
tempImage = [UIImage imageWithCIImage:ciImage];
The problem is I need to crop tempImage
to a specific CGRect
and the only way I know how to do this is by using CGImage
.
The problem is that in the iOS 6.0 documentation I found this:
问题是我需要裁剪tempImage
到一个特定的CGRect
,我知道如何做到这一点的唯一方法是使用CGImage
. 问题是在 iOS 6.0 文档中我发现了这个:
CGImageIf the UIImage object was initialized using a CIImage object, the value of the property is NULL.
CG图像If the UIImage object was initialized using a CIImage object, the value of the property is NULL.
A. How to convert from CIImage to CGImage? I'm using this code but I have a memory leak (and can't understand where):
A. 如何从 CIImage 转换为 CGImage?我正在使用此代码,但出现内存泄漏(无法理解在哪里):
+(UIImage*)UIImageFromCIImage:(CIImage*)ciImage {
CGSize size = ciImage.extent.size;
UIGraphicsBeginImageContext(size);
CGRect rect;
rect.origin = CGPointZero;
rect.size = size;
UIImage *remImage = [UIImage imageWithCIImage:ciImage];
[remImage drawInRect:rect];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
remImage = nil;
ciImage = nil;
//
return result;
}
回答by Joris Kluivers
See the CIContext
documentation for createCGImage:fromRect:
请参阅createCGImage:fromRect的CIContext
文档:
CGImageRef img = [myContext createCGImage:ciImage fromRect:[ciImage extent]];
From an answer to a similar question: https://stackoverflow.com/a/10472842/474896
来自对类似问题的回答:https: //stackoverflow.com/a/10472842/474896
Also since you have a CIImage
to begin with, you could use CIFilter
to actually crop your image.
此外,由于您有一个CIImage
开始,您可以CIFilter
用来实际裁剪您的图像。
回答by skymook
Swift 3, Swift 4and Swift 5
斯威夫特 3、斯威夫特 4和斯威夫特 5
Here is a nice little function to convert a CIImage
to CGImage
in Swift.
这是一个很好的小函数,CIImage
可以CGImage
在 Swift 中将a 转换为。
func convertCIImageToCGImage(inputImage: CIImage) -> CGImage? {
let context = CIContext(options: nil)
if let cgImage = context.createCGImage(inputImage, from: inputImage.extent) {
return cgImage
}
return nil
}
回答by tagyro
After some googling I found this method which converts a CMSampleBufferRef to a CGImage:
经过一些谷歌搜索后,我发现了这个将 CMSampleBufferRef 转换为 CGImage 的方法:
+ (CGImageRef)imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer // Create a CGImageRef from sample buffer data
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0); // Lock the image buffer
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); // Get information of the image
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
/* CVBufferRelease(imageBuffer); */ // do not call this!
return newImage;
}
(but I closed the tab so I don't know where I got it from)
(但我关闭了标签,所以我不知道我从哪里得到的)