How to apply Perspective 3d Transform on image and

2019-04-12 05:13发布

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.

UIView+Quadrilateral

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

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();

and Second using 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();

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.

1条回答
一夜七次
2楼-- · 2019-04-12 06:10

You can do it by CoreImage Framework provided by Apple

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

Swift version

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;
}
查看更多
登录 后发表回答