Image picker crash - iOS Swift - ios

I have some code that will select an image from either the user's photo library or from their camera and display it in an image view. I'm accessing the photo URL from the following line of code:
let pickedPhotoURL: URL = (info["UIImagePickerControllerImageURL"] as? URL)!
This works perfectly when accessing a photo from the user's library. However, I'll always get a nil value at this line why trying to access the photo via the camera. Anyone have any ideas / suggestions?

It's because that value isn't set for a camera. Use UIImagePickerControllerOriginalImage (or edited image if you are using that).
That key gives you a UIImage -- if you need a URL, you need to save it somewhere.
Also, don't use strings, there are keys defined for you.

Related

How to pass imageView as source to QuickLook preview

After picking an image from UIImagePickerController, I am trying to pass that imageView as a source to QuickLook preview which requires URL as a data source.
I am not interested to write the image into any document storage to create a URL, which will create a copy image again in the disk.
Note:
I am using a scanner SDK where it will return a UIImage as a result, so i need to use that UIImage for QuickLook preview.
Any suggestion to achieve this?
You might want to checkout this method (https://developer.apple.com/documentation/uikit/uiimagepickercontrollerdelegate/1619126-imagepickercontroller). One of the arguments to this method is an 'info' dictionary which can be used to get the image URL. Use the key 'imageURL'.

Unable to setup images from JSON?

I'm using Firebase Database & Firebase Storage. I have an iOS Swift app, that allows the user to select multiple images, upload them to Firebase Storage, after the upload, it gets the downloaded URLs and stores them like:
In order to display those images in the app, I have the following xib file:
Where the middle (big) UIImageView is inside of a UIStackView because it could display more than 1 image.
My problem is that I get the URLs from the database, but for some reason I'm not able to display them in the app. I use the framework SDWebImage for caching and displaying images. So far in the app, it worked fine, but not here. I don't get where I'm wrong.
if let photoURLString = post?.photoURLs
{
for photo in photoURLString
{
let photoURL = URL(string: photo)
print(photoURL!)
postImage.sd_setImage(with: photoURL)
//postImagesStackView.addArrangedSubview(postImage)
}
}
post is the model object. I'm using for in to loop through the array of images from the database. When I print each url, I get the actual URL. But it's simple not displayed. I get this. The images are nowhere to be found/displayed:
Try to add aspect ratio for UIImage. Ex: In xib file, set ratio of UIImage to 1.

Uploading photos to e-Mail from Swift

I am writing an app that has a simple form with a photo upload tool, the user uploads their photo to the form and it is displayed in an image view, i then want that image to be in the blank e-Mail that opens up upon clicking submit. I am using the following code to try and achieve this:
let messageBody = "\n\nPhoto: \(imageView.image!)"
The photo gets uploaded and shows in the image view fine but the email displays the following where the image is supposed to be " size {1334, 70} orientation 0 scale 1.000000"
Any help would be greatly appreciated!
Your code (or my previous answer) won't work because you are converting the image to a string (messageBody). And it basically shows you a string of information about the image. So your only option is to write the message in HTML (and set isHTML to true). The good news is that it isn't too hard.
I think the code below should work.
let messageBody = "<html><body>Photo:<img src=\"yourImage.png\"></body></html>"

Take photo and store in custom album in Xamarin.iOS

I want to take a photo and save them within a custom album on a iPad.
I tried a lot of things but ended always with wrong image orientation.
My current procedure is:
Take photo with Xamarin.Media and safe image somewhere in my app path
Create custom album if it's not existing
Load image from temporary path and save to the album
Add the image (asset) additionally to the custom album created in 2.
The workflow above is fully working except of saving the correct orientation. After loading an image with library.AssetForUrl() the containing orientation is every time up. Also after converting from asset -> CGImage -> UImage (uiImage.Orientation).
In step 3 I tried to set the orientation the the appropriate one with
var image = new UIImage(tempImage.CGImage, 1.0f, UIImageOrientation.MyOrientation)
and also with the save command
library.WriteImageToSavedPhotosAlbum(image.CGImage, myOrientation, SaveCompletionBlock)
But the orientation is still ignored. I checked this by opening the Album application as well as loading the file in my self written gallery app.
I want to avoid the use of the iOS picker to take photos. This will produce a mess of code!
Or is there any third party library which allows to take photos and store them in a custom album? Which takes care of all the metadata information? It doesn't matter if it's written in objective-c or c#. I will create bindings for it. But it's awful to work with the camera and albums in iOS.
There are tons of hand-made UIImagePickerController alternatives. Some of them:
https://github.com/w5mith/WSAssetPickerController
https://github.com/chute/photo-picker-plus-ios
https://github.com/B-Sides/ELCImagePickerController

Save photo to camera roll with specific filename

What I try to achieve is to take a photo and let the user specify if the photo belongs to category 1 or category 2. I have 2 buttons, one for each category. When the user presses the button, the photo is saved to the camera roll. AFterwards, at home, when I sync my iPad with iPhoto, I want to be able to sort by category, or at least identify to with category it belongs...
I first thought of the following :
1) taking a photo using the iPad camera -- this is already done in my app
2) saving the photo in camera roll under a specific name, such as cat1_pict123.jpg or cat2_pict456.jpg
I would plan to use
UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, nil );
but I don't know how / or if it is possible to specify a file name ?
Alternatively, is there a way to "tag" a photo before saving it to camera roll ? A comment or something else, which will be visible in iPhoto ?
Thank you for your help
You can't use file names for images saved in photolibrary.
Save that to Document directory with specific name.
There is a way to kinda do that, by setting the image IPTC metadata:
Set the field "Object Name" to set the image title in
iPhoto.
Set the field "Keywords" to set the keywords in
iPhoto, then use a smart album to see all the images with these
keywords.
See details (and code) at http://ootips.org/yonat/how-to-set-the-image-name-when-saving-to-the-camera-roll/ .
That's not possible right now.

Resources