Swift 3 - 如何在应用程序中使用照片库图片?
Swift 3 - How do I use a photolibrary picture in an app?
如何在我的应用程序中使用照片库中的图片?我已经完成了有关创建照片应用程序的教程。一切正常。我有一台UIPickerController
,可以用相机拍张照片,保存到图库中,或者select从图库中截取一张图片,然后放到屏幕上。但是...
我要的是用户select一张图片,记住图片的名字或者编号什么的,然后在另一个页面上从图库中使用这张图片。
喜欢select头像。将图片保存在某处,每当用户进入个人资料页面时,打开之前 selected 的头像图片。所以我需要的是图片的 "name"(represenation),但是我找不到。我该怎么做?
(下面的代码只是工作 cam/lib 应用程序部分,但它保存了没有名称的图片,这就是问题所在:如何找出之前保存的图片?)P.S .抱歉,变量名称是荷兰语。
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var plaatje: UIImageView!
@IBAction func saveknop(_ sender: UIButton) {
let imageData = UIImageJPEGRepresentation(plaatje.image!, 0.6)
let compressedfoto = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedfoto!, nil, nil, nil)
saveNotice()
}
@IBAction func cameraknop(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func biepknop(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
plaatje.image = image
self.dismiss(animated: true, completion: nil)
}
func saveNotice() {
let alert = UIAlertController(title: "foto genomen", message: "je foto is bewaard in de biep", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
}
}
我找到的一个解决方案是使用 UIImageJPEGRepresentation 或 UIImagePNGRepresentation 将图像转换为数据对象。从那里您可以使用 write(to:) 方法将其保存到您希望应用使用的特定 URL。
SO 问题的答案显示了几种存储图像的方法。可能会有帮助。
我之前遇到过同样的问题,这对我有用:
使用以下方法保存图像:
func saveImage(image: UIImage, path: String ) {
let pngImageData = UIImagePNGRepresentation(image)
do {
try pngImageData?.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
print(error)
}
}
使用以下方式加载保存的图像:
func loadImageFromName(name: String) -> UIImage? {
let path = self.fileInDocumentsDirectory(filename: name)
let image = UIImage(contentsOfFile: path)
if image == nil {
print("Image not available at: \(path)")
}
return image
}
func fileInDocumentsDirectory(filename: String) -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
return documentsFolderPath.appendingPathComponent(filename)
}
像这样调用保存方法:
saveImage(image: image, path: fileInDocumentsDirectory(filename: "someId"))
祝你好运!希望这有帮助。
根据这些建议,我找到了另一种解决方案,它出奇地简单。但它有效!诡异的。它不是最好的,但现在我会使用它,直到我了解 iphone.
上的目录和文件系统的所有内容
P.S。归功于:http://theswiftguy.com/index.php/2016/11/10/how-to-save-an-image-locally-in-xcode-8-swift-3-0/
//Encoding
let image = UIImage(named: "dog.png")
let imageData:NSData = UIImagePNGRepresentation(image!)! as NSData
//Saved image
UserDefaults.standard.set(imageData, forKey: "savedImage")
//Decode
let data = UserDefaults.standard.object(forKey: "savedImage") as! NSData
myImageView.image = UIImage(data: data as Data)
如何在我的应用程序中使用照片库中的图片?我已经完成了有关创建照片应用程序的教程。一切正常。我有一台UIPickerController
,可以用相机拍张照片,保存到图库中,或者select从图库中截取一张图片,然后放到屏幕上。但是...
我要的是用户select一张图片,记住图片的名字或者编号什么的,然后在另一个页面上从图库中使用这张图片。
喜欢select头像。将图片保存在某处,每当用户进入个人资料页面时,打开之前 selected 的头像图片。所以我需要的是图片的 "name"(represenation),但是我找不到。我该怎么做?
(下面的代码只是工作 cam/lib 应用程序部分,但它保存了没有名称的图片,这就是问题所在:如何找出之前保存的图片?)P.S .抱歉,变量名称是荷兰语。
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var plaatje: UIImageView!
@IBAction func saveknop(_ sender: UIButton) {
let imageData = UIImageJPEGRepresentation(plaatje.image!, 0.6)
let compressedfoto = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedfoto!, nil, nil, nil)
saveNotice()
}
@IBAction func cameraknop(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
@IBAction func biepknop(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
plaatje.image = image
self.dismiss(animated: true, completion: nil)
}
func saveNotice() {
let alert = UIAlertController(title: "foto genomen", message: "je foto is bewaard in de biep", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(defaultAction)
present(alert, animated: true, completion: nil)
}
}
我找到的一个解决方案是使用 UIImageJPEGRepresentation 或 UIImagePNGRepresentation 将图像转换为数据对象。从那里您可以使用 write(to:) 方法将其保存到您希望应用使用的特定 URL。
我之前遇到过同样的问题,这对我有用:
使用以下方法保存图像:
func saveImage(image: UIImage, path: String ) {
let pngImageData = UIImagePNGRepresentation(image)
do {
try pngImageData?.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
print(error)
}
}
使用以下方式加载保存的图像:
func loadImageFromName(name: String) -> UIImage? {
let path = self.fileInDocumentsDirectory(filename: name)
let image = UIImage(contentsOfFile: path)
if image == nil {
print("Image not available at: \(path)")
}
return image
}
func fileInDocumentsDirectory(filename: String) -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
return documentsFolderPath.appendingPathComponent(filename)
}
像这样调用保存方法:
saveImage(image: image, path: fileInDocumentsDirectory(filename: "someId"))
祝你好运!希望这有帮助。
根据这些建议,我找到了另一种解决方案,它出奇地简单。但它有效!诡异的。它不是最好的,但现在我会使用它,直到我了解 iphone.
上的目录和文件系统的所有内容P.S。归功于:http://theswiftguy.com/index.php/2016/11/10/how-to-save-an-image-locally-in-xcode-8-swift-3-0/
//Encoding
let image = UIImage(named: "dog.png")
let imageData:NSData = UIImagePNGRepresentation(image!)! as NSData
//Saved image
UserDefaults.standard.set(imageData, forKey: "savedImage")
//Decode
let data = UserDefaults.standard.object(forKey: "savedImage") as! NSData
myImageView.image = UIImage(data: data as Data)