Obtain NSURL from UIImagePickerController
I had trouble getting the info[UIImagePickerControllerOriginalImage]
to work. I had to do it like this:
let assets = PHAsset.fetchAssetsWithALAssetURLs([localUrl], options: nil)
let asset = assets.firstObject
asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
let imageFile = contentEditingInput?.fullSizeImageURL
// now call putFile with imageFile instead of localURL
Getting URL of UIImage from UIImagePickerController
Apple has changed something in their NSString
and NSURL
library in their latest release (iOS 9), but those methods are available from iOS 4. You can check the related Apple Forum Post for more details.
For fixing this error, you need to change the code like:
Swift 2:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!)
{
let imageUrl = editingInfo[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.URLByAppendingPathComponent(imageName!)
let image = editingInfo[UIImagePickerControllerOriginalImage]as! UIImage
let data = UIImagePNGRepresentation(image)
data!.writeToFile(localPath.absoluteString, atomically: true)
self.dismissViewControllerAnimated(true, completion: nil);
}
Swift 3:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let imageName = imageUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
let image = info[UIImagePickerControllerOriginalImage]as! UIImage
let data = UIImagePNGRepresentation(image)
do
{
try data?.write(to: localPath!, options: Data.WritingOptions.atomic)
}
catch
{
// Catch exception here and act accordingly
}
self.dismiss(animated: true, completion: nil);
}
Reference:
- lastPathComponent
- URLByAppendingPathComponent:
Getting URL of UIImage selected from UIImagePickerController
Okay, I've solved the issue.
All you have to do is simply grab the image (info[UIImagePickerControllerOriginalImage] as UIImage
) and save to the given directory. If you need to save only 1 picture it works great. The code id below.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
let imageName = imageURL.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let localPath = documentDirectory.stringByAppendingPathComponent(imageName)
let image = info[UIImagePickerControllerOriginalImage] as UIImage
let data = UIImagePNGRepresentation(image)
data.writeToFile(localPath, atomically: true)
let imageData = NSData(contentsOfFile: localPath)!
let photoURL = NSURL(fileURLWithPath: localPath)
let imageWithData = UIImage(data: imageData)!
picker.dismissViewControllerAnimated(true, completion: nil)
}
NSURL of an image from UIImagePicker
While Zhengjie's answer might work. I found a more simple way to go about it:
let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingPathComponent("imagetoupload"))
let data = UIImagePNGRepresentation(image)
data.writeToURL(fileURL!, atomically: true)
uploadReq.body = fileURL
Related Topics
Swift: How to Create a Predicate with an Int Value
Global Var VS Shared Instance Swift
Swift Optional Type: How .None == Nil Works
How to Add Icon to a Share Sheet in Swift
If-Let Any to Rawrepresentable<String>
Play Segment of Avaudiopcmbuffer
Swift Seems to Be Slower as Objective-C in Loops
Observe Progress of Data Download in Swift
Xcode: Using Custom Fonts Inside Dynamic Framework
Setting Nsunderlinestyle Causes Unrecogognized Selector Exception
Swift Programmatically Create Function for Button with a Closure
How Set Custom Fonts for iOS13 Context Menu Actions
Why Can't I Use Subscripting on a Ckrecord Object in Swift
How to Use Tabs to Evenly Space Out Description Strings in Swift
Saving Highscores with Nsuserdefaults
Swift 3 - How to Write Functions with No Initialisers Like the New Uicolors