-1

I am attempting to understand the following code and how you would convert it to Swift. Specifically, I understand this adds an instance method you can call on an instance of CIImage. My question is, how you can do the same thing in a Swift class?

This code is taken from AAPLAssetViewController.m in Apple's example app using the Photos framework.

@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

Call it like so:

NSData *jpegData = [myCIImage aapl_jpegRepresentationWithCompressionQuality:0.9f];
Jordan H
  • 52,571
  • 37
  • 201
  • 351

1 Answers1

0

From The Swift Programming Language - Extensions:

Extensions add new functionality to an existing class, structure, or enumeration type. (...) Extensions are similar to categories in Objective-C.

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

Siyu Song
  • 897
  • 6
  • 21