次のコードと、それを Swift に変換する方法を理解しようとしています。具体的には、これにより、 のインスタンスで呼び出すことができるインスタンス メソッドが追加されることを理解していますCIImage
。私の質問は、Swift クラスで同じことをどのように行うことができるかということです。
このコードはAAPLAssetViewController.m
、Photos フレームワークを使用する Apple のサンプル アプリから取得したものです。
@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];