如何在我的应用程序中保存图像(路径)? [英] How to save an image (path) within my app?

查看:126
本文介绍了如何在我的应用程序中保存图像(路径)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序,用户填写某种形式,并附加图像。用户从库中选择一张图片(使用UIImagePickerController)。我已经知道如何用 info [UIImagePickerControllerOriginalImage]在屏幕上显示图像? UIImage

I want to make an app, where the user fills out some kind of form, and attaches an image to it. The user picks a picture form the gallery (using UIImagePickerController). I already know how to display the image on the screen with info[UIImagePickerControllerOriginalImage] as? UIImage.

我想创建一个文件,该文件将在下次打开应用程序时使用,因此可以显示相同的图像笔记。所以我最好保存文件的路径,这样下次我只需用 UIImage(contentsOfFile:path)打开它。我怎么能得到这条路呢?

I want to make a file which will be used next time the app is opened, so the same image can be displayed with the notes. So preferably I want to save the path to the file so the next time I simply open it with UIImage(contentsOfFile: path). How can I get this path?

到目前为止我有一些不那么有趣的代码:

Some not so interesting code I have so far:

@IBAction func chooseFromGallery(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
        imagePicker.allowsEditing = false
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    dismissViewControllerAnimated(true, completion: nil)

    self.image.image = info[UIImagePickerControllerOriginalImage] as? UIImage  // This works
    self.imagePath =  // What goes here?
    println(self.imagePath)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}






编辑



经过一些阅读后,我发现无法获取保存在应用程序沙箱外部的图像的路径。所以每个人都在寻找相同的东西,停下来,并使用 Bluehound 的答案。

有关如何在iOS(和OS X)中处理文件的更多信息阅读: https://developer.apple。 com / library / ios / documentation / FileManagement / Conceptual / FileSystemProgrammingGuide / FileSystemOverview / FileSystemOverview.html

推荐答案

一个好的存储图像的位置是文档目录。您可以保存到它,然后从中检索图像。我使用这些UIImage扩展来保存和检索图像:

A good place to store images is the documents directory. You can save to it and then retrieve images from it. I use these UIImage extensions for saving and retrieving images:

extension UIImage {
func save(fileName: String, type: String) {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

        if type.lowercaseString == "png" {
            let path = "\(documentsPath)/\(fileName).\(type)"
            UIImagePNGRepresentation(self).writeToFile(path, atomically: true)
        } else if type.lowercaseString == "jpg" {
            let path = "\(documentsPath)/\(fileName).\(type)"
            UIImageJPEGRepresentation(self, 1.0).writeToFile(path, atomically: true)
        } else {

        }
    }

convenience init?(fileName: String, type: String) {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
        let path = "\(documentsPath)\(fileName).\(type)"
        self.init(contentsOfFile: path)
    }
}

使用这个,我可以使用一些数据存储方法(如Core Data)存储文件名而不是图像的整个路径。

Using this, I can store a file name rather than the entire path of an image using some data storing method such as Core Data.

使用:

someImage.save("SomeName" , type: "png")

if let image = UIImage(fileName: "SomeName", type: "png") {
}

这篇关于如何在我的应用程序中保存图像(路径)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆