gpt4 book ai didi

ios - 如何保存照片和文字

转载 作者:可可西里 更新时间:2023-11-01 02:19:27 25 4
gpt4 key购买 nike

我有一张图片和一段文字。如果用户点击安全按钮,我想保存它。

@IBOutlet weak var img1: UIImageView!
@IBOutlet weak var text1: UITextField!
@IBAction func saveButton(sender: AnyObject) {
save()
}

我可以使用 func 快照保护 View 。但是如何保存文本、img 和快照?

我尝试了功能保存。我在 func 保存中出错:“使用未解析的标识符对象”“使用未解析的标识符 imageView”

func save() {
var object = Object( text: textField.text!, image:
imageView.image, image: image)
}

func snapshot() -> UIImage {
UIGraphicsBeginImageContext(self.view.frame.size)
self.view.drawViewHierarchyInRect(self.view.frame,
afterScreenUpdates: true)
let image : UIImage =
UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return image
}

最佳答案

方法如下:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var newImage: UIImageView!
@IBOutlet weak var img1: UIImageView!

@IBAction func saveIt(sender: AnyObject) {
//first make an UIImage from your view
UIGraphicsBeginImageContext(self.view.bounds.size)
self.view.layer.renderInContext(UIGraphicsGetCurrentContext())
let sourceImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

//draw new Image
//position the image
UIGraphicsBeginImageContext(img1.frame.size)
sourceImage.drawAtPoint(CGPointMake(0, 0))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//assign new Image
newImage.image = croppedImage
}
}

结果将是:

enter image description here

关于ios - 如何保存照片和文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com