ios - 如何使用 Swift 将 ImageView 正确定位在照片顶部

标签 ios swift merge draw photos

目前,我可以保存顶部图像的照片,但是顶部图像并不位于预览 View 中出现在屏幕上时的坐标。

fileprivate func mergeUIViews( ) -> UIImage?
{
    let bottomImage = photo
    let topImage    = uiViewInstance.image

    let bottomImageHeight = bottomImage.size.height
    let bottomImageWidth  = bottomImage.size.width

    let topImageHeight = uiViewInstance.frame.height
    let topImageWidth  = uiViewInstance.frame.width
    let topImageOrigin = uiViewInstance.frame.origin

    let bottomImageSize = CGSize(width: bottomImageWidth, height: bottomImageHeight)
    let topImageSize = CGSize(width: topImageWidth, height: topImageHeight)

    // Merge images

    UIGraphicsBeginImageContextWithOptions(bottomImageSize, false, 0.0)

    bottomImage.draw(in: CGRect(origin: CGPoint.zero, size: bottomImageSize))
    topImage   .draw(in: CGRect(origin: topImageOrigin, size: topImageSize)) // Where I believe the problem exists
    let newImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return newImage
}

最佳答案

有效的方法不是使用实际捕获的照片的大小,而是将照片大小设置为 View Controller 的 View 大小。这导致了整个屏幕的所需输出,显示合并的照片以及覆盖图像的原点的正确点。

fileprivate func mergeUIViews( ) -> UIImage?
{
    let bottomImage = photo
    let topImage    = uiViewInstance.image

    let bottomImageSize = self.view.frame.size

    let topImageHeight = uiViewInstance.frame.height
    let topImageWidth  = uiViewInstance.frame.width
    let topImageOrigin = uiViewInstance.frame.origin
    let topImageSize   = uiViewInstance.frame.size

    // Merge images

    UIGraphicsBeginImageContextWithOptions(bottomImageSize, false, 0.0)

    bottomImage.draw(in: CGRect(origin: CGPoint.zero, size: bottomImageSize))
    topImage   .draw(in: CGRect(origin: topImageOrigin, size: topImageSize)) // Where I believe the problem exists
    let newImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return newImage
}

关于ios - 如何使用 Swift 将 ImageView 正确定位在照片顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687420/

相关文章:

ios - Swift:如何检测平移手势何时被解除/取消?

git - git如何比较文件?

sql - 合并重叠的时间间隔,如何?

sql - 如何在 MERGE INTO 语句中使用 JOIN?

iphone - 如何使表达式在 iOS 上可点击?

ios - iOS 应用程序的 Jenkins 代码签名失败

ios - 找不到图书馆 |链接器命令失败

swift - 如何从另一个类调用 perform segue 方法?

ios - 更改默认的 iOS slider

ios - 在我使用 instantiateViewController 函数的情况下在 View 之间传递数据