将新缩放的图像保存到PhotoLibrary

时间:2017-12-04 23:41:43

标签: swift3 uiimage scale photolibrary

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

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

//触摸缩放图像操作

@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.

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

1 个答案:

答案 0 :(得分:0)

AHA!我得到了它,我创建了另一个Subview,并使我的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。

现在它可以工作,但它看起来像垃圾。它的分辨率非常低。我必须弄清楚如何拍摄这张快照并让它成为设备的分辨率。