I realise that this question may be a little broad, but basically all I want to do is use image picker to select a photo by using a camera or within the photo library, and saving that photo to an array. I already know how to populate a collection view with images from assets folder. Just really how to save user images to an array. How do I name each image?
Also, it is preferable if the array could be a global one, so that these images can be accessed elsewhere.
Thanks in advance for your help.
Why not simple use:
var myImages = [UIImage]()
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let yourPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImages.append(yourPickedImage)
}
dismissViewControllerAnimated(true, completion: nil)
}
Edit:
Save Image to Disk:
func saveImage (image: UIImage, path: String ) -> Bool{
let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
let result = jpgImageData!.writeToFile(path, atomically: true)
return result
}
func getDocumentsURL() -> NSURL {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
return documentsURL
}
func fileInDocumentsDirectory(filename: String) -> String
let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
return fileURL.path!
}
How to store
let imagePath = fileInDocumentsDirectory(myImageName)
saveImage(yourPickedImage, path: imagePath)
Read
func loadImageFromPath(path: String) -> UIImage? {
let image = UIImage(contentsOfFile: path)
return image
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.