如何在图像上应用Perspective 3d变换并将该图像保存在文档目录中 [英] How to apply Perspective 3d Transform on image and save that image in document directory
本文介绍了如何在图像上应用Perspective 3d变换并将该图像保存在文档目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过跟踪代码在图像层上应用3D变换.
I am applying 3D transform on a image layer by follow code.
UIImageView *view = (UIImageView *)[recognizer view];
CGPoint translation = [recognizer translationInView:self.view];
CGPoint newCenter = view.center;
newCenter.x += translation.x;
newCenter.y += translation.y;
view.center = newCenter;
[recognizer setTranslation:CGPointZero inView:self.view];
以下是将点转换为变换的起点.
Following is the from which the points are converted to transform.
[self.view transformToFitQuadTopLeft:self.topLeftControl.center
topRight:self.topRightControl.center
bottomLeft:self.bottomLeftControl.center
bottomRight:self.bottomRightControl.center];
为了将转换图像保存到UIImage,我使用了以下代码,并使用renderInContext
.
For Saving the transform Image to UIImage i have used following code using renderInContext
.
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
和第二个使用drawViewHierarchyInRect
UIGraphicsBeginImageContextWithOptions(self.toothImageView_1.bounds.size, NO, 0);
BOOL ok = [self.toothImageView_1 drawViewHierarchyInRect:self.toothImageView_1.bounds afterScreenUpdates:YES];
UIImage *img = nil;
if( ok )
{
image = UIGraphicsGetImageFromCurrentImageContext();
}
self.imageView.image = image;
UIGraphicsEndImageContext();
我正在获取原始的带框方形图像,而不是转换后的图像.实际上,我需要将3d变换图像保存在文档目录中.
I am getting a original framed square image not transformed image. I actually require a 3d transform image to be saved in the document directory.
先谢谢了.任何建议和答案都是最欢迎的.
Thanks in advance. Any suggestion and answers are most welcome.
推荐答案
您可以通过
快速版本
func imageByTransformingImage(image: UIImage?) -> UIImage? {
guard image != nil else {
return nil;
}
if let coreImage = CIImage(data: UIImagePNGRepresentation(image!)!) {
//assuming that topLeft, topRight, bottomRight and bottomLeft stored in transformationDict
let dict = self.transformationDict.valueForKey("Corners");
let dict = [String : Any]();
let topLeftPoint = CGPointFromString(dict["TopLeftCorner"] as! String);
let topRightPoint = CGPointFromString(dict["TopRightCorner"] as! String);
let bottomRightPoint = CGPointFromString(dict["BottomRightCorner"] as! String);
let bottomLeftPoint = CGPointFromString(dict["BottomLeftCorner"] as! String);
if let perspectiveTransformation = CIFilter(name: "CIPerspectiveTransform") {
perspectiveTransformation.setValue(CIVector(CGPoint: topLeftPoint), forKey: "inputTopLeft");
perspectiveTransformation.setValue(CIVector(CGPoint:topRightPoint), forKey: "inputTopRight");
perspectiveTransformation.setValue(CIVector(CGPoint:bottomRightPoint), forKey: "inputBottomRight");
perspectiveTransformation.setValue(CIVector(CGPoint:bottomLeftPoint), forKey: "inputBottomLeft");
perspectiveTransformation.setValue(coreImage, forKey:kCIInputImageKey);
if let resultImage = perspectiveTransformation.outputImage {
let ciContext = CIContext()
let cgImageRef = ciContext.createCGImage(resultImage, fromRect: resultImage.extent) as CGImageRef
return UIImage(CGImage: cgImageRef);
//transformed image will be flipped image you need to flip context to get correct UIImage
}
}
}
return nil;
}
这篇关于如何在图像上应用Perspective 3d变换并将该图像保存在文档目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文