我正在尝试理解以下代码以及如何将其转换为 Swift。具体来说,我知道这会添加一个实例方法,您可以在CIImage
. 我的问题是,如何在 Swift 课程中做同样的事情?
此代码取自AAPLAssetViewController.m
Apple 使用 Photos 框架的示例应用程序。
@implementation CIImage (Convenience)
- (NSData *)aapl_jpegRepresentationWithCompressionQuality:(CGFloat)compressionQuality {
static CIContext *ciContext = nil;
if (!ciContext) {
EAGLContext *eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
ciContext = [CIContext contextWithEAGLContext:eaglContext];
}
CGImageRef outputImageRef = [ciContext createCGImage:self fromRect:[self extent]];
UIImage *uiImage = [[UIImage alloc] initWithCGImage:outputImageRef scale:1.0 orientation:UIImageOrientationUp];
if (outputImageRef) {
CGImageRelease(outputImageRef);
}
NSData *jpegRepresentation = UIImageJPEGRepresentation(uiImage, compressionQuality);
return jpegRepresentation;
}
@end
像这样称呼它:
NSData *jpegData = [myCIImage aapl_jpegRepresentationWithCompressionQuality:0.9f];