Converting .obj file data to NSImage - swift

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.

Related

Converting UIImage to Data throws unsupported file format

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())!)

create image from data cocoa swift 5

I have a set of Data coming from a request (AlamoFire) as per the image below:
I know it's an image, the size of the data is correct. I'd like to create an image from this and tried:
NSImage(data: result) or
NSImage.init(data:result)
Without success. It is possible to do what I try to achieve?
EDIT: to answer Larme on Self.coverImage:
did you try to use this code snippet?
// Swift
// Image to data
let image = UIImage(named: "sample")
let data = image?.pngData()
let data = image?.jpegData(compressionQuality: 0.9)
// Data to Image
let uiImage: UIImage = UIImage(data: imageData)
// this method is deprecated
var imageData: Data = UIImagePNGRepresentation(image)

An image become large in size when it is shared

I am taking an image by device camera and add an autoAdjustmentFilters. Then I compress it's size and save to document directory. Now problem is when I am sharing this image by airdrop, image becomes larger in size. I am facing this problem when I am sharing the image in JPEG format but not in PDF format. Related code is given below:
let aCGImage = inputImage.CGImage;
self.aCIImage = CIImage(CGImage: aCGImage!)
let Filters = self.aCIImage.autoAdjustmentFilters() as [CIFilter]
for Filter in Filters {
Filter.setValue(self.aCIImage, forKey: kCIInputImageKey)
self.outputImage = Filter.outputImage!
}
let CgImage = self.context.createCGImage(self.outputImage, fromRect: self.outputImage.extent)
self.newUIImage = UIImage(CGImage: CgImage!)
let mydata = UIImageJPEGRepresentation(self.newUIImage, 0.5)
print(">>>>>>>>>>>>,,,,,,,,,\((mydata?.length)! / 1024) kb")
Then I save this image data in document directory. When I save this image in document directory, it's size is 600kb-1500kb, but after sharing it's size is larger than 10000kb. Don't understand what the problem is.

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 get and save GIF Image from NSImageView to disk

Currently I am working on an NSImageView, on which the user drags and drops an image, and the images gets saved to the disk. I am able to save png and jpeg images, but while saving GIF images, all that is saved is a single frame from the gif. The imageView is able to display the whole animated gif though.
My current implementation to save image from NSImageView to disk is:
let cgRef = image.CGImageForProposedRect(nil, context: nil, hints: nil)
let newRep = NSBitmapImageRep(CGImage: cgRef!)
newRep.size = image.size
let type = getBitmapImageFileType(imageName.lowercaseString) // getBitmapImageFileType: returns NSBitmapImageFileType
let properties = type == .NSGIFFileType ? [NSImageLoopCount: 0] : Dictionary<String, AnyObject>()
let data: NSData = newRep.representationUsingType(type, properties: properties)!
data.writeToFile(link, atomically: true)
How should I modify this code to be able to save all the frames of GIF to the disk.