iOS:Swift:如何使用CGImageCreateWithImageInRect获得正确的图像质量?

时间:2016-01-02 20:20:02

标签: ios swift uiimage

我正在尝试使用Swift创建一个简单的Crop功能。我正在尝试使用CGImageCreateWithImageInRect函数 - 它工作得很好,但产生的质量很差。我错过了什么吗?

 func retriveCroppedImage(){
    let yratio: CGFloat = imgviewrect.size.height / chosenImage.size.height
    let xratio: CGFloat = imgviewrect.size.width / chosenImage.size.width

    var cliprect = CGRectMake(centerpoint.x - vWidth/2, centerpoint.y - vHeight/2, vWidth, vHeight)
    print("cliprect top  \(cliprect.size)")
    cliprect.size.height =  cliprect.size.height / xratio;
    cliprect.size.width =  cliprect.size.width / xratio;
    cliprect.origin.x = cliprect.origin.x / xratio + imgviewrect.origin.x  / xratio
    cliprect.origin.y = cliprect.origin.y / yratio - imgviewrect.origin.y  / xratio

    print("cliprect  On Image \(cliprect)")

    let imageRef =  CGImageCreateWithImageInRect(chosenImage.CGImage, cliprect )

    croppedImg  = UIImage(CGImage: imageRef!, scale:  UIScreen.mainScreen().scale, orientation: chosenImage.imageOrientation)
    print("Operation complete");
}

截屏:Main VC

裁剪后我得到Cropped Image

1 个答案:

答案 0 :(得分:0)

在尝试了所有选项后 - 我偶然发现我在故事板上的图像视图中设置了Alpha。 CGImageCreateWithImageInRect函数没有任何问题。现在我的裁剪应用程序正在按预期工作。但是谢谢大家的建议。