Objective c 裁剪UIImage时CGImageCreateWithImageInRect出现问题

Objective c 裁剪UIImage时CGImageCreateWithImageInRect出现问题,objective-c,swift,uiimage,crop,cgimage,Objective C,Swift,Uiimage,Crop,Cgimage,我知道关于这件事有很多线索,但我仍然找不到解决我的问题的答案 我从PHAsset对象中获取UIImage,然后按如下方式裁剪图像 使用此代码: let fImage = preview.image!.fixOrientation() let newSize = CGSizeMake(img!.width * (cropFrame.width / previewFrame.width),img!.height * ( cropFrame.height / previewFram

我知道关于这件事有很多线索,但我仍然找不到解决我的问题的答案

我从
PHAsset
对象中获取UIImage,然后按如下方式裁剪图像

使用此代码:

 let fImage = preview.image!.fixOrientation()
        let newSize = CGSizeMake(img!.width * (cropFrame.width / previewFrame.width),img!.height * ( cropFrame.height / previewFrame.height))
        let pX = (fabs((previewFrame.width - cropFrame.width)) / previewFrame.width) * img!.width
        let py = (fabs((previewFrame.height - cropFrame.height)) / previewFrame.width) * img!.width

        var newRect = CGRect()
        newRect.size = newSize
        newRect.origin = CGPointMake(pX, py)

        let imageRef: CGImageRef = CGImageCreateWithImageInRect(fImage.CGImage, newRect)!
        let newImage = UIImage(CGImage: imageRef, scale: fImage.scale, orientation: preview.image!.imageOrientation )
其中,
fixorination

 func fixOrientation() -> UIImage {

        // No-op if the orientation is already correct
        if ( self.imageOrientation == UIImageOrientation.Up ) {
            return self;
        }

        // We need to calculate the proper transformation to make the image upright.
        // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
        var transform: CGAffineTransform = CGAffineTransformIdentity

        if ( self.imageOrientation == UIImageOrientation.Down || self.imageOrientation == UIImageOrientation.DownMirrored ) {
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
        }

        if ( self.imageOrientation == UIImageOrientation.Left || self.imageOrientation == UIImageOrientation.LeftMirrored ) {
            transform = CGAffineTransformTranslate(transform, self.size.width, 0)
            transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
        }

        if ( self.imageOrientation == UIImageOrientation.Right || self.imageOrientation == UIImageOrientation.RightMirrored ) {
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform,  CGFloat(-M_PI_2));
        }

        if ( self.imageOrientation == UIImageOrientation.UpMirrored || self.imageOrientation == UIImageOrientation.DownMirrored ) {
            transform = CGAffineTransformTranslate(transform, self.size.width, 0)
            transform = CGAffineTransformScale(transform, -1, 1)
        }

        if ( self.imageOrientation == UIImageOrientation.LeftMirrored || self.imageOrientation == UIImageOrientation.RightMirrored ) {
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
        }

        // Now we draw the underlying CGImage into a new context, applying the transform
        // calculated above.
        let ctx: CGContextRef = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height),
            CGImageGetBitsPerComponent(self.CGImage), 0,
            CGImageGetColorSpace(self.CGImage),
            CGImageGetBitmapInfo(self.CGImage).rawValue)!;

        CGContextConcatCTM(ctx, transform)

        if ( self.imageOrientation == UIImageOrientation.Left ||
            self.imageOrientation == UIImageOrientation.LeftMirrored ||
            self.imageOrientation == UIImageOrientation.Right ||
            self.imageOrientation == UIImageOrientation.RightMirrored ) {
                CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage)
        } else {
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage)
        }

        // And now we just create a new UIImage from the drawing context and return it
        return UIImage(CGImage: CGBitmapContextCreateImage(ctx)!)
    }
然而结果是


有什么建议吗?谢谢

很明显,您的
新直线原点
不正确,因此我想了解您如何计算
pX
pY
@konrad.bajtyngier-Tho如果(0,0)位于左上方(与UIKit一样),我正在计算比例距离((max x-origin x)/maxX),使用imgX的倍数应该给我们提供真实图像上的正确原点?好吧,快速看一眼,您使用的是宽度和高度,而不是原点,并且在计算时使用的是宽度pY@konrad.bajtyngier哦,哇,很抱歉(第二部分)。你能详细说明一下第一部分吗?(使用宽度和高度而不是原点)如果要计算尺寸,则使用
cropFrame
的宽度和高度是正确的。但在计算原点时,您将无法得到正确的结果。改用它的来源。