-1

我正在尝试理解以下代码以及如何将其转换为 Swift。具体来说,我知道这会添加一个实例方法,您可以在CIImage. 我的问题是,如何在 Swift 课程中做同样的事情?

此代码取自AAPLAssetViewController.mApple 使用 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];
4

1 回答 1

0

来自Swift 编程语言 - 扩展

扩展向现有的类、结构或枚举类型添加新功能。(...) 扩展类似于 Objective-C 中的类别。

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

于 2015-01-24T20:08:10.300 回答