0

This routine returns nil in OSX 10.13.2 Beta but not in a playground. Don't have an older OS to test with at the moment.

func getImage(_ url: CFURL) -> CGImage? {
    let sourceOptions = [
        kCGImageSourceShouldAllowFloat  as String: kCFBooleanTrue as NSNumber
    ]  as CFDictionary

    guard let imageSource = CGImageSourceCreateWithURL(url, sourceOptions) else { return nil }

    guard let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil) else {
        let imageSourceStatus = CGImageSourceGetStatus(imageSource)
        Swift.print("image nil, Image Source Status = \(imageSourceStatus)")
        return nil
    }
    return image
}

imageSource is non-nil, but image is nil. The console message is "Image Source Status = CGImageSourceStatus", which is not one of the valid enum values.

Tried with both Swift 3.2 and Swift 4.0. The Dictionary arg in CGImageSourceCreateWithURL can be set to nil; nothing changes.

The correct frameworks are in the project (although it linked without them, so I'm not sure they matter.) Did "import ImageIO" but it built without it, so again I'm not sure in matters.

The URL is absolutely a valid file URL -- like I said, this works in the playground, and I've tried a number of different files and file types (tiff, jpg and png).

Any ideas?

  • make sure to turn off Sandboxing in your app – Leo Dabus Nov 20 '17 at 20:31
  • Building for OSX, as far as I can tell there's no such thing as sandbox (searched in Build Settings) – GuyalOfSfere Nov 21 '17 at 19:50
  • App Sandbox in targets capabilities (first switch) – Leo Dabus Nov 21 '17 at 19:52
  • This has an alternative that works for me: https://stackoverflow.com/questions/24595908/swift-nsimage-to-cgimage. I still haven't found the sandbox option. Are you using XCode 9? – GuyalOfSfere Nov 21 '17 at 20:34
  • https://www.dropbox.com/s/4faveqoaa9o49l9/Screen%20Shot%202017-11-21%20at%2018.39.25.png?dl=1 – Leo Dabus Nov 21 '17 at 20:40
  • Leo Dabus' answer is correct. (Thanks!) I didn't quite understand what he meant in his description of XCode 9 navigation -- select the Target, select the Capabilities Tab, and the App Sandbox options are at the top. – GuyalOfSfere Nov 22 '17 at 23:40

0 Answers0