Converting UIImage to Data throws unsupported file format - swift

I'm using the image picker to select a photo from photo library or to take a camera and in my imagePickerController function, I get the UIImage and try to convert it to an Image Data like this
let uiImage = info.[UIImagePickerController.Infokey.originalImage] as! UIImage
let imageData = uiImage.jpegData(compressionQuality: 1)! // this line throws error
And I keep getting this error
findWriterForTypeAndAlternateType:119: unsupported file format 'public.heic'
And also when I try to update my imageview like this Image(uiImage) the view gets updated but I don't know why I'm getting that error.
Can someone tell me what I'm doing wrong?

Okay... I've got it working. I don't know for what reason, but I changed myimageData to this:
let imageData = Data((uiImage.pngData())!)

Related

How can i show Images from Sqlite to a uicollectionview

I have some images in sqlite database. I saved Image in BLOB type (I converted in img.pngdata as NSData and save it to db). Now I want to retrieve images and show them in a uicollectionview view. when I retrieve image it is like this
1. Which type of array should I use to show them in collection view? The table in the db is like this2.
Also, how can I get the image name as a string from the retrieved image?
I solved the issue by retrieving the blob data like this:
let lenght:Int = Int(sqlite3_column_bytes(queryStatement, 0));
let imgdata : NSData = NSData(bytes: sqlite3_column_blob(queryStatement, 0), length: lenght)
let decoded_img : UIImage = UIImage(data: imgdata as Data)!
imageNames.append(decoded_img)
I declared an array of type UIImage, then used it in uicollectionview view, it is now working fine for me.

Converting .obj file data to NSImage

I'm trying to convert the data from a .obj file to a base64 string, so that I can display a thumbnail preview of the .obj file in an NSImageView.
Below is my code for getting the asset from a local url, and converting it to a base64 string. I then convert that to Data, which I then attempt to convert to an image.
let fileUrl = Foundation.URL(string: input_string)
let data: NSData? = NSData(contentsOfFile: input_string)
if let fileData = data {
let base64String = fileData.base64EncodedString()
let decodedData = NSData(base64Encoded: base64String, options: NSData.Base64DecodingOptions(rawValue: 0) )
let decodedimage = NSImage(data: decodedData! as Data)
}
I can see that I'm getting the data set, but decodedImage is always nil - I'm guessing this has something to do with .obj image format, which I may not know enough about. Any insight / help appreciated.
EDIT: Here's a screenshot showing what the .obj file looks like in the finder preview.
OBJ is not an image format. Its an old 3D model format. If you want to convert it to an image you will need to open the file and add it to a SCNScene as a geometry node and then render out the the SCNView to an image and save that image.

How to send audio file with image and caption in iMessage app for iOS 10?

I am creating iMessage app and trying to send audio or video file to other user.
Video file works and looks fine but its not working as expected with audio file.
My current code is:
let destinationFilename = mp3FileNames[i]
let destinationURL = docDirectoryURL.appendingPathComponent(destinationFilename)
if let conversation = activeConversation {
let layout = MSMessageTemplateLayout()
layout.image = UIImage.init(named: "audio-x-generic-icon")
layout.mediaFileURL = destinationURL
layout.caption = selectedSongObj.name
let message = MSMessage()
message.layout = layout
message.url = URL(string: "emptyURL")
conversation.insert(message, completionHandler: nil)
return
}
Looks like layout.mediaFileURL = destinationURL is not adding any file into message.
And when I try to send file with above code.It looks like shown below:
It looks fine but there is no audio to play but if I try this way:
let destinationFilename = mp3FileNames[i]
let destinationURL = docDirectoryURL.appendingPathComponent(destinationFilename)
if let conversation = activeConversation {
conversation.insertAttachment(destinationURL!, withAlternateFilename: nil, completionHandler: nil)
return
}
And result with above code is:
I can play audio for that message because it's there. But problem with that message is I can not attach any image or caption with it.
How can I attach image and audio file into same message.
And if possible instead of image can I add GIF?
Any help would be much appreciated, Thank you.
Not necessary to use GIF, iMessage extensions supports also PNGand JPEG image formats. Recommended image size is 300x300 points at #3x scale.
If the MSMessageTemplateLayout's image property has a non-nil value then
mediaFileURL property is ignored. So you can't send an image and an audio file at the same time. Docs

How to display local JPG image in a PFImageView

I have several PFImageViews that are there to show images downloaded from Parse. However, there is one local image that I want to be able to load when I desire. For some reason, this code isn't working. Any help is greatly appreciated, thanks!
In cellForRowAtIndexPath:
let checkInImage = UIImage(named: "checkin.jpg")!
let checkInFile = UIImageJPEGRepresentation(checkInImage, 1.0)!
let imagePFFile = PFFile(data: checkInFile)!
tableViewCell.PFImageViewOne.file = imagePFFile
tableViewCell.PFImageViewOne.loadInBackground()
Since PFImageView is a subclass of UIImageView, you should be able to just set the image view's image property to your image like this:
tableViewCell.PFImageViewOne.image = checkInImage

Saving image into core data and fetching it into tableview cell

I did a set up of CoreData with entity "users" and attribute "username" (when I type something in uitextfield it saves into core data and I got readings from core data into tableview cell). That is working like a charm. But I also want to display chosen picture from imagepickercontroller and that is also working fine when I choose picture but when I click save I think I got it saved. I don't know if it is saved because I eliminated all error so I got no errors, but it won't read that picture into tableview cell. I put new attribute "image" with type binary data into core data file but code does not work.
This is code for saving into core data
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)
newUser.setValue(nameField.text, forKey: "username")
let picture = UIImageJPEGRepresentation(photoImageView.image!, 1);
newUser.setValue(picture, forKey: "image")
And this is code where tableview reads data from core data
cell.imageView!.image = person.valueForKey("image") as? UIImage
I also tried with
[UIImage, imageWithData:self.myEvent.picture];
but Swift 2.0 don't have that syntax imageWithData
person.valueForKey("image") as? UIImage won't work.
Use UIImage(data:) to convert the retrieved NSData to UIImage.
for retrieving image from core data i used this and it is finally working.
let image = person.valueForKey("image") as? NSData
cell.imageView!.image = UIImage(data: image!)