web-dev-qa-db-fra.com

Swift 3 et CGContextDrawImage

Je souhaite traduire cette ligne dans le code de la syntaxe actuelle de Swift 3, mais il semble y avoir quelques problèmes:

CGContextDrawImage(context, CGRect(x:0.0,y: 0.0,width: image!.size.width,height: image!.size.height), image!.cgImage)

Selon le CoreGraphics.apinotesCGContextDrawImage a été converti en CGContext.draw:

Name: CGContextDrawImage
  # replaced by draw(_ image: CGImage, in rect: CGRect, byTiling: Bool = false)
  SwiftName: CGContext.__draw(self:in:image:)
  SwiftPrivate: true

Quand j'essaie de faire:

 CGContext.draw(context as! CGImage, in: CGRect(x:0.0, y:0.0, width: image!.size.width, height: image!.size.height), byTiling: false) 

Il semble qu'une syntaxe simple perturbe le compilateur, mais je ne peux pas voir (en fait, je reçois une erreur ambiguë typique):

 enter image description here

Quelqu'un peut-il m'aider avec ce nouveau code de syntaxe Swift 3?

17
Alessandro Ornano

Vous devez l'appeler comme s'il s'agissait d'une méthode d'instance de CGContext:

context.draw(image!.cgImage!, in: CGRect(x: 0.0,y: 0.0,width: image!.size.width,height: image!.size.height))

Vérifiez la dernière référence de CGContext .

36
OOPer

J'ai trouvé une autre très bonne solution à ce problème que j'utilise actuellement. Il vous suffit de passer l'image comme argument à cette méthode après avoir capturé l'image à l'aide de UIImagePickerController. Cela fonctionne bien pour toutes les versions d'iOS et aussi pour les orientations portrait et paysage de l'appareil photo. Il vérifie la propriété EXIF ​​de l'image à l'aide de UIImageOrientaiton et selon la valeur de l'orientation, il transforme et redimensionne l'image afin que vous obteniez la même image de retour avec la même orientation que l'orientation de votre vue caméra.

Ici, j'ai gardé des résolutions maximales de 3000 afin que la qualité d'image ne soit pas gâchée spécialement lorsque vous utilisez des appareils rétines, mais vous pouvez modifier sa résolution selon vos besoins.

func scaleAndRotateImage(image: UIImage, MaxResolution iIntMaxResolution: Int) -> UIImage {
        let kMaxResolution = iIntMaxResolution
        let imgRef = image.cgImage!
        let width: CGFloat = CGFloat(imgRef.width)
        let height: CGFloat = CGFloat(imgRef.height)
        var transform = CGAffineTransform.identity
        var bounds = CGRect.init(x: 0, y: 0, width: width, height: height)

        if Int(width) > kMaxResolution || Int(height) > kMaxResolution {
            let ratio: CGFloat = width / height
            if ratio > 1 {
                bounds.size.width = CGFloat(kMaxResolution)
                bounds.size.height = bounds.size.width / ratio
            }
            else {
                bounds.size.height = CGFloat(kMaxResolution)
                bounds.size.width = bounds.size.height * ratio
            }
        }
        let scaleRatio: CGFloat = bounds.size.width / width
        let imageSize = CGSize.init(width: CGFloat(imgRef.width), height: CGFloat(imgRef.height))

        var boundHeight: CGFloat
        let orient = image.imageOrientation
        // The output below is limited by 1 KB.
        // Please Sign Up (Free!) to remove this limitation.

        switch orient {
        case .up:
            //EXIF = 1
            transform = CGAffineTransform.identity
        case .upMirrored:
            //EXIF = 2
            transform = CGAffineTransform.init(translationX: imageSize.width, y: 0.0)
            transform = transform.scaledBy(x: -1.0, y: 1.0)

        case .down:
            //EXIF = 3
            transform = CGAffineTransform.init(translationX: imageSize.width, y: imageSize.height)
            transform = transform.rotated(by: CGFloat(Double.pi / 2))

        case .downMirrored:
            //EXIF = 4
            transform = CGAffineTransform.init(translationX: 0.0, y: imageSize.height)
            transform = transform.scaledBy(x: 1.0, y: -1.0)
        case .leftMirrored:
            //EXIF = 5
            boundHeight = bounds.size.height
            bounds.size.height = bounds.size.width
            bounds.size.width = boundHeight
            transform = CGAffineTransform.init(translationX: imageSize.height, y: imageSize.width)

            transform = transform.scaledBy(x: -1.0, y: 1.0)
            transform = transform.rotated(by: CGFloat(Double.pi / 2) / 2.0)
            break

        default: print("Error in processing image")
        }

        UIGraphicsBeginImageContext(bounds.size)
        let context = UIGraphicsGetCurrentContext()
        if orient == .right || orient == .left {
            context?.scaleBy(x: -scaleRatio, y: scaleRatio)
            context?.translateBy(x: -height, y: 0)
        }
        else {
            context?.scaleBy(x: scaleRatio, y: -scaleRatio)
            context?.translateBy(x: 0, y: -height)
        }
        context?.concatenate(transform)
        context?.draw(imgRef, in: CGRect.init(x: 0, y: 0, width: width, height: height))
        let imageCopy = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return imageCopy!
    }
0
sajgan2015