gpt4 book ai didi

ios - 如何在图像上应用 Perspective 3d Transform 并将该图像保存在文档目录中

转载 作者:搜寻专家 更新时间:2023-10-30 20:13:57 26 4
gpt4 key购买 nike

我正在通过以下代码在图像层上应用 3D 变换。

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];

下面是点从中转换到transform。

UIView+Quadrilateral

[self.view transformToFitQuadTopLeft:self.topLeftControl.center
topRight:self.topRightControl.center
bottomLeft:self.bottomLeftControl.center
bottomRight:self.bottomRightControl.center];

为了将转换图像保存到 UIImage,我使用了以下代码并使用了 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 转换图像保存在文档目录中。

提前致谢。欢迎提出任何建议和答案。

最佳答案

您可以通过 Apple 提供的 CoreImage 框架来完成

- (UIImage *)imageByTransformingImage:(UIImage *)image {

if (image == nil) {
return nil; //image not found
}
CIImage *coreImage = [CIImage imageWithData:UIImagePNGRepresentation(image)];

if (!coreImage) {
return nil; // image is not converted in core image
}

//assuming that topLeft, topRight, bottomRight and bottomLeft stored in transformationDict
NSDictionary *dict = [self.transformationDict valueForKey:@"Corners"];

CGPoint topLeftPoint = CGPointFromString([dict valueForKey:kTopLeft]);
CGPoint topRightPoint = CGPointFromString([dict valueForKey:kTopRight]);
CGPoint bottomRightPoint = CGPointFromString([dict valueForKey:kBottomRight]);
CGPoint bottomLeftPoint = CGPointFromString([dict valueForKey:kBottomLeft]);

CIFilter *perspectiveTransformation = [CIFilter filterWithName:@"CIPerspectiveTransform"];
[perspectiveTransformation setValue:[CIVector vectorWithCGPoint:topLeftPoint] forKey:@"inputTopLeft"];
[perspectiveTransformation setValue:[CIVector vectorWithCGPoint:topRightPoint] forKey:@"inputTopRight"];
[perspectiveTransformation setValue:[CIVector vectorWithCGPoint:bottomRightPoint] forKey:@"inputBottomRight"];
[perspectiveTransformation setValue:[CIVector vectorWithCGPoint:bottomLeftPoint] forKey:@"inputBottomLeft"];
[perspectiveTransformation setValue:coreImage forKey:kCIInputImageKey];

CIImage *resultImage = [perspectiveTransformation outputImage];
CIContext *ciContext = [CIContext contextWithOptions:nil];
CGImageRef cgImageRef = [ciContext createCGImage:resultImage fromRect:[resultImage extent]];
UIImage *transformedImage = [UIImage imageWithCGImage:cgImageRef];

return transformedImage;
//transformed image will be flipped image you need to flip context to get correct UIImage
}

快速版本

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;
}

关于ios - 如何在图像上应用 Perspective 3d Transform 并将该图像保存在文档目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41484121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com