当前位置: 首页 > 面试题库 >

使用Swift裁剪图像并将其置于中心位置

韶兴德
2023-03-14
问题内容

在Swift编程中,如何裁剪图像,然后将其放在中心?

到目前为止,这就是我要做的…我已经成功裁剪了图像,但是我想在之后将其放在中间

ImgView.image = OrigImage
var masklayer = CAShapeLayer()
masklayer.frame = ImgView.frame
masklayer.path = path.CGPath
masklayer.fillColor = UIColor.whiteColor().CGColor
masklayer.backgroundColor = UIColor.clearColor().CGColor

ImgView.layer.mask = masklayer

UIGraphicsBeginImageContext(ImgView.bounds.size);
ImgView.layer.renderInContext(UIGraphicsGetCurrentContext())
var image = UIGraphicsGetImageFromCurrentImageContext()
ImgView.image = image
UIGraphicsEndImageContext();

更新:

let rect: CGRect = CGRectMake(path.bounds.minX, path.bounds.minY, path.bounds.width, path.bounds.height)

// Create bitmap image from context using the rect
let imageRef: CGImageRef = CGImageCreateWithImageInRect(image.CGImage, rect)
ImgView.bounds = rect
ImgView.image = UIImage(CGImage: imageRef)

我可以通过获取path.bound和size来居中,并更改ImageView的边界。:)


问题答案:

要获得作物的居中位置,可以将高度和宽度的差减半。然后,您可以在检查图像的方向(较长的部分)后为新的宽度和高度指定边界

func cropToBounds(image: UIImage, width: Double, height: Double) -> UIImage {

    let contextImage: UIImage = UIImage(CGImage: image.CGImage)!

    let contextSize: CGSize = contextImage.size

    var posX: CGFloat = 0.0
    var posY: CGFloat = 0.0
    var cgwidth: CGFloat = CGFloat(width)
    var cgheight: CGFloat = CGFloat(height)

    // See what size is longer and create the center off of that
    if contextSize.width > contextSize.height {
        posX = ((contextSize.width - contextSize.height) / 2)
        posY = 0
        cgwidth = contextSize.height
        cgheight = contextSize.height
    } else {
        posX = 0
        posY = ((contextSize.height - contextSize.width) / 2)
        cgwidth = contextSize.width
        cgheight = contextSize.width
    }

    let rect: CGRect = CGRectMake(posX, posY, cgwidth, cgheight)

    // Create bitmap image from context using the rect
    let imageRef: CGImageRef = CGImageCreateWithImageInRect(contextImage.CGImage, rect)

    // Create a new image based on the imageRef and rotate back to the original orientation
    let image: UIImage = UIImage(CGImage: imageRef, scale: image.scale, orientation: image.imageOrientation)!

    return image
}

我在本网站上找到了大部分此类信息,以防您想进一步阅读。

Swift 4* 更新 *

func cropToBounds(image: UIImage, width: Double, height: Double) -> UIImage {

        let cgimage = image.cgImage!
        let contextImage: UIImage = UIImage(cgImage: cgimage)
        let contextSize: CGSize = contextImage.size
        var posX: CGFloat = 0.0
        var posY: CGFloat = 0.0
        var cgwidth: CGFloat = CGFloat(width)
        var cgheight: CGFloat = CGFloat(height)

        // See what size is longer and create the center off of that
        if contextSize.width > contextSize.height {
            posX = ((contextSize.width - contextSize.height) / 2)
            posY = 0
            cgwidth = contextSize.height
            cgheight = contextSize.height
        } else {
            posX = 0
            posY = ((contextSize.height - contextSize.width) / 2)
            cgwidth = contextSize.width
            cgheight = contextSize.width
        }

        let rect: CGRect = CGRect(x: posX, y: posY, width: cgwidth, height: cgheight)

        // Create bitmap image from context using the rect
        let imageRef: CGImage = cgimage.cropping(to: rect)!

        // Create a new image based on the imageRef and rotate back to the original orientation
        let image: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)

        return image
    }


 类似资料:
  • 问题内容: 如何使用JavaFX 完成Android ? 我有图像视图: 问题答案: 感谢@TravisF的发布,我实现了最后一个解决方案,以使图像始终具有相同的高度,宽度和位置(中心)。

  • 我试图在从图库中选择图像后使用intent来裁剪图像。以下是我的代码片段 在这里,我使用PICK_IMAGE_REQUEST意图句柄调用上面的代码段 由于我在裁剪后使用了相同的意图,即PICK_IMAGE_REQUEST,可能会出现什么问题

  • 我是图像处理新手,开始学习scikit图像。我试图检测矩形的角,然后裁剪整个图像。但我完全迷失在大量的分割和检测算法中,不知道我需要哪种算法以及如何去做。 此代码生成一个示例图像。我想把它裁剪成绿色的矩形。我需要做什么? 从matplotlib将pyplot导入为pyplot 任务是检测矩形(多边形阵列)的边缘并将图像裁剪到其中。 我尝试了哈里斯角检测,精明的边缘检测和许多其他,但我完全困惑。这似

  • 问题内容: 我正在从磁盘读取图像,并将其显示在一行中。图像文件大于行中需要显示的图像文件。由于我需要将RAM 缓存在内存中以加快访问速度,因此我希望它们的大小只能与s 一样大(85x85 dip) 现在我正在读文件 位图= BitmapFactory.decodeFile(file); 而ImageView负责缩放和裁剪它 android:scaleType =“ centerCrop” AFAI

  • 问题内容: 下面的代码可以很好地裁剪图像,这是我想要的,但是对于较大的图像,它也可以正常工作。有什么办法可以缩小图像吗? 想法是,在裁剪之前,我将能够使每个图像的大小大致相同,以便每次都能获得良好的效果 代码是 问题答案: 如果要生成缩略图,则必须首先使用调整图像大小。您必须调整图像的大小,以使图像较小侧的尺寸等于拇指的相应侧。 例如,如果源图像为1280x800px,拇指为200x150px,则

  • 本文向大家介绍在Python中使用OpenCV裁剪图像,包括了在Python中使用OpenCV裁剪图像的使用技巧和注意事项,需要的朋友参考一下 什么是裁剪? 裁剪是从摄影或插图图像中去除不需要的外部区域。该过程通常包括去除图像的某些外围区域,以从图片中去除多余的垃圾,改善其取景,改变纵横比,或使主题与背景突出或分离。 我们将使用 OpenCV-python (cv2)的这些函数, imread()