I'm currently working on a photo app written in Swift. I'm having a problem getting the EXIF data from the photos. I found some tutorials here but couldn't find one in Swift that worked for me. Here is my code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as UIImage
let imageData = UIImageJPEGRepresentation(image, 1.0)
//Assign image to a variable
selectedImage = image
self.dismissViewControllerAnimated(true, completion: nil)
println("finished")
}
How can I get the EXIF data (specifically, the GPS data) from the UIImage? Thanks!
Related
In iOS Swift I'm having difficulty loading an animated GIF from the device photo library to a UIImageView or creating a .gif file from it. I have no problem displaying the animated GIF if it's a file on a server or if it's right in the app. However, when I load it from the photo library, I lose the animation. I found some Objective-C solutions and tried to convert them over to Swift, but haven't had any luck.
Here is what I have so far:
#IBAction func buttonTapped(sender: UIButton) {
selectPhoto()
}
func selectPhoto() {
let photoLibraryController = UIImagePickerController()
photoLibraryController.delegate = self
photoLibraryController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
let mediaTypes:[String] = [kUTTypeImage as String]
photoLibraryController.mediaTypes = mediaTypes
photoLibraryController.allowsEditing = false
self.presentViewController(photoLibraryController, animated: true, completion: nil)
}
// UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let origImage = info[UIImagePickerControllerOriginalImage] as! UIImage
self.imageView.image = origImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
Optimally, I would like to save the photo library image to a gif file on the server. From there I have no trouble displaying it. However, I need some help getting the data from the photo library into some type of property, without losing the animation. Do I need to use something like ALAssetsLibrary for this?
Thanks.
Yes, Use ALAssetsLibrary → now called PHAsset.
You should get the NSData of the gif, not UIImage( because UIImage will only get the first frame.)
So basically you would do something like this:
One you get the asset
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true // adjust the parameters as you wish
PHImageManager.default().requestImageData(for: asset, options: requestOptions, resultHandler: { (imageData, UTI, _, _) in
if let uti = UTI,let data = imageData ,
// you can also use UTI to make sure it's a gif
UTTypeConformsTo(uti as CFString, kUTTypeGIF) {
// save data here
}
})
Resource: PHAsset
You can get an access to the location of the image file using InfoKey .imageURL and retrieve Data from this URL
// UIImagePickerControllerDelegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let path = info[.imageURL] as? URL {
if let data = try? Data(contentsOf: path) {
self.imageView.image = UIImage(data: data)
//Now you have Data of a file: so you can convert it to image or gif or send to server or start animation (e.g. Framework JellyGIF)
self.imageView.startGif(with: .data(data))
}
picker.dismissViewControllerAnimated(true, completion: nil)
}
Hi I am new to pdfkit and iOS applications, I want to take a picture with device and then convert the image to pdf. taking pictures with camera and saving it on Camera Roll works completely fine, the problem is when I want to create pdf from that image it needs the name of the image, how I can find it?
and another question is it possible to implement any pdf editor with pdfkit to edit image?
Appreciate any help. below is my imagePickerController codes.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
if (Mylibrary)
{
}
else
{
UIImageWriteToSavedPhotosAlbum(pickedImage!, nil, nil, nil);
let image = UIImage(named: "????") //the problem is here
let newPage = PDFPage(image: image!)
}
picker.dismiss(animated: true, completion: nil)
}
There's no need to instantiate a new image since the image is already stored in the pickedImage variable. You just need to initialize the new PDFPage object with pickedImage. Use ImageKit to edit the image before using it to initialize a new PDFPage object.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
// Adds pickedImage to the user’s Camera Roll album.
UIImageWriteToSavedPhotosAlbum(pickedImage, nil, nil, nil)
// Creates a new PDFPage object and initializes it with pickedImage.
let newPage = PDFPage(image: pickedImage)
dismiss(animated: true, completion: nil)
}
Can we load images found locally on the device with SDWebimage using URL option? if yes then Im trying to use SDWebimage to display images, below is my code and i am unable to load image in the imageview. its still blank. kindly help me to understand what im doing wrong?
func addImage(sender: AnyObject){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
picController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
picController.allowsEditing = true
self.presentViewController(picController, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
titleImageView.sd_setImageWithURL(url)
self.dismissViewControllerAnimated(true, completion: nil)
}
Note that the UIImagePickerController delegate
imagePickerController: didFinishPickingMediaWithInfo
will have the info as described in documentation-
A dictionary containing the original image and the edited image, if an
image was picked; or a filesystem URL for the movie, if a movie was
picked. The dictionary also contains any relevant editing information.
The keys for this dictionary are listed in Editing Information Keys.
so if it is image that you are looking at, it will be right there in the dictionary itself.
You can get original image directly-
let image = info[UIImagePickerControllerOriginalImage]
if let image = image {
//Set this image to your image view directly
titleImageView.image = image
}
Here info as a NSDictonary and get that key is UIImagePickerControllerOriginalImage
see this code :
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage
imagePreview.image = tempImage
self.dismissModalViewControllerAnimated(true)
}
I am using either the camera or the library app to take/select an image
eg.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// User has selected an image
// To dismiss the image picker
self.dismissViewControllerAnimated(true, completion: nil)
// Select the Image of interest
// retrieve the object via key from documentation
self.imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
If I set a breakpoint at self.imageView.image and step down, I can get the image properties but I don't know how to get the filename.
I'm having an issue whereby images that were taken by a Sony digital camera and transferred to my phone return nil when I try to save the image in didFinishPickingMediaWithInfo. It returns an image for any image taken on my iPhone or downloaded from the Internet. Why is this and what can I do to fix it?
allowsEditing is set to false.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
dismissViewControllerAnimated(true, completion: nil) //5
var chosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage //2
let previewVC:ImagePreviewViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PreviewVC") as! ImagePreviewViewController
previewVC.imageToCrop = chosenImage!
self.navigationController?.pushViewController(previewVC, animated: true)
}
UPDATE
I discovered that this issue relates to my iCloud Photo Library sync and I selected a raw image (.arw) instead of JPEG. The raw images return nil