[英]Getting a CGImage from CIImage
I have a UIImage which is loaded from a CIImage with: 我有一个从CIImage加载的UIImage:
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
. 问题是我需要将tempImage
裁剪为特定的CGRect
,而我知道如何执行此操作的唯一方法是使用CGImage
。 The problem is that in the iOS 6.0 documentation I found this: 问题是在iOS 6.0文档中我发现了这个:
CGImage CGImage
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? A.如何从CIImage转换为CGImage? I'm using this code but I have a memory leak (and can't understand where): 我正在使用此代码,但我有内存泄漏(并且无法理解在哪里):
+(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;
}
See the CIContext
documentation for createCGImage:fromRect: 请参阅CIContext
文档:fromRect:
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
来实际裁剪你的图像。
Swift 3 , Swift 4 and Swift 5 Swift 3 , Swift 4和Swift 5
Here is a nice little function to convert a CIImage
to CGImage
in Swift. 这是一个很好的小函数,可以在Swift CGImage
CIImage
转换为CGImage
。
func convertCIImageToCGImage(inputImage: CIImage) -> CGImage? {
let context = CIContext(options: nil)
if let cgImage = context.createCGImage(inputImage, from: inputImage.extent) {
return cgImage
}
return nil
}
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) (但我关闭了标签,所以我不知道从哪里得到它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.