2016-01-25 5 views
0

Как я могу использовать NSDocument для программной обработки изображений в программе Swift iOS?Как использовать NSDocument для хранения изображений

let alertController = UIAlertController(title: "Choose an option", message: "", preferredStyle: .Alert) 

    let cameraRollAction = UIAlertAction(title: "Camera Roll", style: .Default) { (action) in 
     self.imagePicker.delegate = self 
     self.imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum; 
     self.imagePicker.allowsEditing = true 
     self.presentViewController(self.imagePicker, animated: true, completion: nil) 

    } 
    alertController.addAction(cameraRollAction) 

    let takePictureAction = UIAlertAction(title: "Take a picture", style: .Default) { (action) in 
     self.imagePicker.delegate = self 
     self.imagePicker.allowsEditing = true 
     self.imagePicker.sourceType = UIImagePickerControllerSourceType.Camera 
     self.imagePicker.cameraCaptureMode = .Photo 
     self.imagePicker.modalPresentationStyle = .FullScreen 
     self.presentViewController(self.imagePicker, 
      animated: true, 
      completion: nil) 
    } 

Мне нужно сохранить изображения при выборе кнопки и получить их при необходимости.

+0

видеть эту ссылку [сохранить] (http://stackoverflow.com/questions/28066688/how-to-save-an-image-path-within-my- app) и [retrieve] (http://stackoverflow.com/questions/29005381/get-image-from-documents-directory-swift) –

ответ

0

реализации делегата UIImagePickerControllerDelegate

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

let image = info[UIImagePickerControllerOriginalImage] as? UIImage 
let imageData = UIImagePNGRepresentation(image!) 
let dirPaths = NSSearchPathForDirectoriesInDomains(
    .DocumentDirectory, .UserDomainMask, true)[0] as NSString 

    let fullPath = dirPaths.stringByAppendingPathComponent("yourNameImg.png") 
    let result = imageData.writeToFile(fullPath, atomically: true) 
    dismissViewControllerAnimated(true, completion: nil) 

} 
+0

Он показывает ошибку «stringByAppendingPathComponent» недоступен: вместо этого используйте URLByAppendingPathComponent в NSURL. " –

+0

сейчас попробуйте это. и дайте мне знать, если у вас возникнут какие-либо проблемы. – Sahil

+0

Когда я выбираю изображение, оно застревает. –

Смежные вопросы