Swift NSImage à CGImage
Comment puis-je convertir un NSImage à CGImage dans Swift? En Objective-C, je l'ai fait comme ceci:
- (CGImageRef)CGImage {
NSData *imageData = self.TIFFRepresentation;
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CGImageRef maskRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
return maskRef;
}
Maintenant, j'ai essayé avec:
extension NSImage {
var CGImage: CGImageRef {
get {
let imageData = self.TIFFRepresentation
let source = CGImageSourceCreateWithData(imageData as CFDataRef, nil)
let maskRef = CGImageSourceCreateImageAtIndex(source, UInt(0), nil)
return maskRef;
}
}
}
Je ne peux pas compiler, j'obtiens le message d'erreur: Could not find an overload for 'init' that accepts the supplied arguments'
à la ligne let maskRef ...
Vous devez vous connecter pour publier un commentaire.
Voici ce que j'utilise pour convertir NSImage à CGImage:
nil
pourproposedRect
et obtenir le même résultat.Ah, j'ai trouvé la solution. C'est parce que dans Swift vous un objet non géré (je ne comprends pas vraiment ce que cela signifie). Mais ce code fonctionne maintenant:
When Swift imports APIs that have not been annotated, the compiler cannot automatically memory manage the returned Core Foundation objects. Swift wraps these returned Core Foundation objects in an Unmanaged<T> structure. All indirectly returned Core Foundation objects are unmanaged as well.
Extrait De: Apple Inc. “L'utilisation de Swift avec Cocoa et Objective-C.” iBooks. itunes.apple.com/ru/book/using-swift-cocoa-objective/...Swift 3 et 4 de la version de code :-
Pour Swift 4.0, XCode 9.2: