-1

次のコードと、それを 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];
4

1 に答える 1

0

The Swift Programming Language - Extensionsから:

拡張機能は、既存のクラス、構造体、または列挙型に新しい機能を追加します。(...) 拡張機能は、Objective-C のカテゴリに似ています。

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html

于 2015-01-24T20:08:10.300 に答える