如何快速拍摄照片并将其保存到本地应用程序 [英] How to take a picture and save it to the app locally with swift

查看:130
本文介绍了如何快速拍摄照片并将其保存到本地应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,在当前阶段,当用户拍照时,图片将存储在应用程序本地。

I'm working on an app where in the current stage when a user takes a picture the picture will be stored locally within the app.

 @IBAction func CameraAction(sender: UIButton) {
let picker = UIImagePickerController()
    picker.delegate = self
    picker.sourceType = .Camera
    picker.allowsEditing = true
    self.presentViewController(picker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    picker.dismissViewControllerAnimated(true, completion: nil)

    //Save image
    let img = UIImage()
    let data = UIImagePNGRepresentation(img)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "myImageKey")
    NSUserDefaults.standardUserDefaults().synchronize()
    NSLog("Image stored?")
    viewDidLoad()


}

通过上面的内容,我的iPhone打开了相机,可以拍照了。当我单击捕获和使用此图像选项时,我认为图像实际上并未存储,因为我无法调用这些图像。

From what I have above, my iPhone opens up its camera and I can take a picture. When I hit capture and the "use this image" option I don't think it the pictures are actually being stored because I haven't been able to recall the pictures.

因此,我在以下方面寻求帮助:如何在本地存储从UIImagePickerController拍摄的图片(可能在documents文件夹中),如何调用它以及如何无NSUserDEfaults 。谢谢。

So I'm asking for help in how to locally store a picture taken from the UIImagePickerController, possibly in the documents folder, how to recall it, and how to do it without NSUserDEfaults. Thanks.

推荐答案

这是将图像存储在NSUserDefaults中的方式:

This is how you would store an image in NSUserDefaults:


正如我在评论中提到的那样,不建议这样做,如果用于多个图像,可能会导致严重的性能损失甚至崩溃。

As I mentioned in my comment, this is not recommended and, if used for more than one image, may result in serious performance loss and even crashes.


let image = UIImage(named: "myImage")
let pngImage = UIImagePNGRepresentation(image) 
            
NSUserDefaults.standardUserDefaults().setObject(pngImage, forKey: "image")
            
            

并检索图像:

var retrievedImage = NSUserDefaults.standardUserDefaults().objectForKey("image") as! AnyObject

然后显示图像:

imageView.image = UIImage(data: retrievedImage as! NSData)



如何将图像存储到文件中(更好):

This is how to store an image to a file (much better):


func saveImage (image: UIImage, path: String ) -> Bool{
    
    let pngImageData = UIImagePNGRepresentation(image)
    //let jpgImageData = UIImageJPEGRepresentation(image, 1.0)   // if you want to save as JPEG
    let result = pngImageData!.writeToFile(path, atomically: true)
    
    return result
    
}

您将这样称呼它:

saveImage(image, path: imagePath)

然后,使用以下函数来检索图像:

Then, to retrieve the image, use this function:

func loadImageFromPath(path: String) -> UIImage? {
    
    let image = UIImage(contentsOfFile: path)
    
    if image == nil {
        
        print("missing image at: \(path)")
    }
    print("Loading image from path: \(path)") // this is just for you to see the path in case you want to go to the directory, using Finder.
    return image
    
}

您可以这样称呼它:

image = loadImageFromPath(imagePath)
        imageView.image = image

有关更多详细信息,请参见 http:// helpmecodeswift。 com / image-manipulation / Saving-loading-images

For more details, take a look at http://helpmecodeswift.com/image-manipulation/saving-loading-images


此外,您实际上并没有使用在示例中选择的图像,正如Frederik指出的那样。

Also, you don't actually use the image that gets selected in your example, as Frederik pointed out.

您保存了一个未分配的空变量'img'。您需要保存选择器完成后返回的变量图像。

You save an empty variable 'img' that has not been assigned to. You need to save 'image', the variable you get back after the picker has finished.

以下是获取图像的方式:

此函数返回变量'image'

This function returns the variable 'image'

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
picker.dismissViewControllerAnimated(true, completion: nil)



然后可以继续使用它,例如

You can then go ahead and just use it e.g.


imageView.image = image  

或者也许

 saveImage(image, path: imagePath)  

祝你好运!

这篇关于如何快速拍摄照片并将其保存到本地应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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