2017-09-06 14 views
1

J'ai une forme définie en UIBezierPath. J'ai aussi un UIImage. Je veux maintenant recadrer cette image en utilisant UIBezierPath. Comment puis-je faire ceci ?Rogner une image en utilisant UIBezierPath - Débutant

let shape = UIBezierPath(rect: imageRect) 

let image = UIImage(cgimage: c) 
+0

Toujours pas de chance. Quelqu'un avec une solution? – Illep

+0

est Objc Code ok pour vous? –

+0

Est-ce que ma solution vous est utile? ou vous avez toujours un problème avec ma solution? –

Répondre

-1

Si vous affichez l'image dans un UIImageView, vous pouvez utiliser la propriété mask.

Vous devez faire un UIImage sur le chemin:

extension UIBezierPath { 
    func asMaskImage(fillColor:UIColor = UIColor.black) -> UIImage 
    { 
     // Make a graphics context 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0) 
     let context = UIGraphicsGetCurrentContext() 

     context!.setFillColor(fillColor.cgColor) 

     // and fill it! 
     self.fill() 

     let image = UIGraphicsGetImageFromCurrentImageContext() 

     UIGraphicsEndImageContext() 

     return image! 
    } 
} 

Ensuite, vous utiliser comme masque:

myImageView.image = image 
myImageView.mask = UIImageView(image: shape.asMaskImage()) 
+0

Il est dit 'Impossible d'affecter une valeur de type 'UIImage' à un type 'UIView?'' – Illep

+0

Désolé - J'avais simplifié un autre code. Il doit être mis dans un UIImageView. Voir éditer. – rjpadula

+1

Mais ce n'est pas ce qu'il a demandé. Il a demandé comment masquer un UIImage, pas un UIImageView. – matt

0

Je mettais le code Objective C. Cependant, il est facile de convertir en swift3

S'il vous plaît essayer ci-dessous le code

CAShapeLayer *newLayer = [[CAShapeLayer alloc] init]; 
newLayer.path = self.shape.CGPath; 


[self.view.layer setMask:newLayer]; 

CGRect rectToCrop = self.shape.bounds; 

UIGraphicsBeginImageContext(self.view.frame.size);//, NO, 0); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[newLayer removeFromSuperlayer]; 
newLayer = nil; 

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rectToCrop); 
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.imgVPhoto.image.scale orientation:self.imgVPhoto.image.imageOrientation]; 
CGImageRelease(imageRef); 

Et puis enregistrez

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ 

    //saving cut Image 
    NSData *imgData = UIImagePNGRepresentation(croppedImage); 

    if (imgData) { 

     NSString *imagePath = 

     if([FileUtility fileExistsAtFilePath:imagePath]) { 
      [FileUtility deleteFile:imagePath]; 
     } 

     //need to add this task in BG to avoid blocking of main thread 

     [imgData writeToFile:imagePath atomically:true]; 

    } 

}]; 

espère que cela vous aide à vous

EDIT

SWIFT Convert

var newLayer = CAShapeLayer() 
newLayer.path = shape.cgPath 
view.layer.mask = newLayer 
var rectToCrop: CGRect = shape.bounds 
UIGraphicsBeginImageContext(view.frame.size) 
//, NO, 0); 
view.layer.render(inContext: UIGraphicsGetCurrentContext()) 
var image: UIImage? = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 
newLayer.removeFromSuperlayer() 
newLayer = nil 
var imageRef: CGImageRef? = image?.cgImage.cropping(to: rectToCrop) 
var croppedImage = UIImage(cgImage: imageRef!, scale: imgVPhoto.image.scale, orientation: imgVPhoto.image.imageOrientation) 
CGImageRelease(imageRef)