gpt4 book ai didi

ios - 如何获取 iOS Swift 图片库图像的 URL?

转载 作者:行者123 更新时间:2023-11-29 02:14:26 24 4
gpt4 key购买 nike

我正在为 UIImagePickerController 编写如下代码,以从照片库中获取图像。

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
// Code here

var imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.mediaTypes = [kUTTypeImage]
imagePicker.allowsEditing = false

self.presentViewController(imagePicker, animated: true, completion: nil)
}

选择图像后,我可以在 UIImageView 上显示它,但无法获取它的 URL。

这是我为获取它而编写的代码。

let url: NSString = info[UIImagePickerControllerReferenceURL] as NSString

var imageName:String = url.lastPathComponent
println("URL is \(imageName)")

let image = info[UIImagePickerControllerOriginalImage] as UIImage
profileImage.image = image
self.dismissViewControllerAnimated(true, completion: nil)

这里它给出了nil,如果我使用UIImagePickerControllerEditedImage,它也会崩溃。

我们不能直接访问 URL,如果不是的话 UIImagePickerControllerReferenceURL 应该返回什么?

如何获取图像的 URL,以便将其发送到服务器。

谢谢

最佳答案

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask

if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
if let dirPath = paths[0] as? String {
let readPath = dirPath.stringByAppendingPathComponent("yourNameImg.png")
var pickedimage = UIImage(CGImage: UIImage(contentsOfFile: readPath)!.CGImage, scale: 1.0, orientation: .Up)

UploadImagePreview.image = pickedimage
}
}
}

注意:如果知道完整地址,我们可以从url设置图片。这里我们在 nsDocumentDirectory 中搜索给定的图像名称,找出完整的图像路径,然后设置为 ImageView 。

关于ios - 如何获取 iOS Swift 图片库图像的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28911217/

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