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)
Toujours pas de chance. Quelqu'un avec une solution? – Illep
est Objc Code ok pour vous? –
Est-ce que ma solution vous est utile? ou vous avez toujours un problème avec ma solution? –