How to apply Perspective 3d Transform on image and save that image in document directory

拟墨画扇 提交于 2019-12-04 14:36: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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!