将新缩放的图像保存到 PhotoLibrary

Save newly scaled image to PhotoLibrary

我希望用户能够使用 UIPinchGesture 缩放图像。当图像缩放到他们想要的大小时,我希望能够将新缩放的背景 UIView 作为高分辨率 .png 保存到 PhotoLibrary。

这是我的比例图像代码和我的保存代码:

//触摸刻度图像动作

@IBAction func scaleImage(_ sender: UIPinchGestureRecognizer) {

    myImageView.transform = CGAffineTransform(scaleX: sender.scale, y: sender.scale)
}

//保存图片功能

@IBAction func saveArtwork(_ sender: AnyObject) {


    UIGraphicsBeginImageContext(self.myImageView.bounds.size)

    // The code below may solve your problem
    myImageView.layer.render(in: UIGraphicsGetCurrentContext()!)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let activity = UIActivityViewController(activityItems: [image!], applicationActivities: nil)
    present(activity, animated: true, completion: nil)



}

我正在制作一个应用程序,您可以在其中缩放图像并保存新比例,它可以像您想要的一样大或一样小,如果它超出范围我希望它裁剪出视野之外的东西.如果它是按比例缩小的,我希望保存背景,以便您可以在编辑背景下看到微小的图像。 the original image is an instagram square photo. this is the app where i scale the image to be very small. i would like it to appear just this way with the black background as a new photo that is the same resolution and aspect ratio as the screen. so it will appear to be a black portrait rectangle with the new scaled image.my app saves this image to the library it is low resolution, unscaled and the background it looks to be transparent and appears white. not what i want.

我现在得到的结果是图像看起来是缩放的,然后当我保存它时它居中并裁剪到图像视图左右边界。

啊哈!我明白了,我创建了另一个子视图,并使我的 imageView 成为该视图的子视图,然后为新创建的视图提供了一个出口并将代码更改为:

@IBAction func saveArtwork(_ sender: AnyObject) {

    UIGraphicsBeginImageContext(self.captureView.bounds.size)

    // The code below may solve your problem
    captureView.layer.render(in: UIGraphicsGetCurrentContext()!)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    let activity = UIActivityViewController(activityItems: [image!], applicationActivities: nil)
    present(activity, animated: true, completion: nil)
}

新视图是 captureView。

现在可以用了,但看起来很糟糕。这是非常低的分辨率。我必须弄清楚如何拍摄此快照并将其设置为设备的分辨率。