Lumialxk Lumialxk - 1 month ago 107
Swift Question

'-[CIContext initWithOptions:]: unrecognized selector sent to instance

I used this to generate a big image:

let context = CIContext(options: nil)
let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)!
CGContextSetInterpolationQuality(bitmapRef, CGInterpolationQuality.None)
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
return UIImage(CGImage: scaledImage)


It worked well in iOS 9 and 10 but not 8. I got this in the debugger:


Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[CIContext initWithOptions:]:
unrecognized selector sent to instance 0x7f868d5dc8e0'


In addition. I tried to use
let context = CIContext()
instead. But I got nil in the second line. I'm using Xcode 8 and Swift 2.3. Please help me with this! Thanks!

Answer

I have no idea if this will work, but we should try it: let's write that line in Objective-C. So:

ContextMaker.h

#import <Foundation/Foundation.h>
#import <CoreImage/CoreImage.h>

@interface ContextMaker : NSObject

+ (CIContext*) makeMeAContext;

@end

ContextMaker.m

#import "ContextMaker.h"

@implementation ContextMaker

+ (CIContext*) makeMeAContext {
    return [CIContext contextWithOptions:nil];
}

@end

Bridging Header:

#import "ContextMaker.h"

Swift:

let c = ContextMaker.makeMeAContext()

It will only take you a moment to try it, so give it a go and see if we can get past that troublesome line...

Comments