What OSX image format is compatible with IOS - ios

I am storing OSX icons (NSImage) in core data as Transformable attributes. IOS is unable to decode the NSImage it seems.
What are my options for storing a compatible image in Core Data, using a transformable (or other) attribute and accessing the image on iOS.
Currently the user can drop their own image onto the NSImageWell and this is stored in the objective-c class as an NSImage. This obviously gets archived into the core data transformable and when iOS tried to retrieve it it throws an exception because it does not understand an NSImage object.
Is there something other than NSImage I can use on OS X that will be compatible with iOS ?

PNG (= Portable Network Graphics) is an option.
You can convert an NSImage to PNG data with (copied from How to save a NSImage as a new file):
NSImage *image = ...;
NSBitmapImageRep *imgRep = [[image representations] objectAtIndex: 0];
NSData *pngData = [imgRep representationUsingType: NSPNGFileType properties: nil];
and create an UIImage from PNG data with
UIImage *image = [UIImage imageWithData:pngData];

Related

Bridging NSImage and UIImage

I have an iphone application that can be enriched with additional content downloadable in the form of packages.
This additional content (packages) is built using a MAC OS application.
The MAC OS application is used to gather resources and artwork, do the layout, and create the package.
Some of the package contains serialized objects (NSCoder), amongst which are NSImage objects.
My issue is : NSImage does not exist on iPhone.
Is it possible to deserialize an NSImage into an UIImage ?
Convert NSImage to NSData and then this NSData is to UIImage.
NOTE : Save image data to file to read from it
MAC CODE
NSData *imageData = [self.someImage TIFFRepresentation];
iOS CODE
UIImage *image = [UIImage imageWithData: imageData];
You could store the images as Data (it is Encodable) on macOS, and then instantiate the UIImage on iOS by doing:
let image = UIImage(data: yourData) // yields UIImage?

Getting nil image even though the file exists in directory

I am saving an image from to my apps directory, and when I try to retrieve it with imageWithContentsOfFile it returns a nil image. I have verified that the file exists in the apps container and that the path I use is correct. Any ideas?
If there is any code that you need let me know. I am not sure what you would need to see.
Thanks
BTW here is the exact syntax I use for loading the image from the path.
UIImage *image = [UIImage imageWithContentsOfFile:path];
The first thing you should do is to check is it a file input/output issue or an image issue. So try to read your file data with
NSData *fileData = [NSData dataWithContentsOfFile:path];
If fileData is nil or its length is 0 then you should recheck the path and the way you are storing a data.
If fileData is ok try
image = [[UIImage alloc] initWithData:fileData];
to check a data to image conversion. Is it possible to open file with some MacOS image viewer?

Why [UIImage imageWithData:] supports PVRTC format?

I suddenly find out that in iOS 8.3 SDK (I didn't test other versions), I can use [UIImage imageWithData:] to load PVRTC format directly?
It isn't supposed to be like that, right? I can't find any documentation or discussion about it, I don't even know if I can rely on it...
But it does work in both simulators and real devices.
Here are some codes of my test, the test project doesn't include OpenGLES.framework.
NSData *data = THE_CONTENT_OF_A_PVR_FILE;
NSString *tempDir = NSTemporaryDirectory();
[data writeToFile:[tempDir stringByAppendingPathComponent:#"temp.pvr"] atomically:YES];
UIImage *tempImage = [UIImage imageWithData:data];
[UIImagePNGRepresentation(tempImage) writeToFile:[tempDir stringByAppendingPathComponent:#"temp.png"] atomically:YES];
The two saved files are shared here: http://d.pr/f/17ugQ
You can check the pvr file in a HEX editor, it coresponds to the specifications of PVRTC format version 2, with pixel format in PVRTC4.
Any idea?

Save Exif data to Image inside Documents folder on iOS device

I have UIImage *photoImage containing jpg file. I would like to add EXIF data to this image like UserComment, ImageDescription and Software. I would like to save this file to Documents folder on my iOS device as myImage.jpg I don't want to save it to PhotoLibrary.
I am not sure how to add EXIF data.
I would save image to disk by using
NSData *photoData = UIImageJPEGRepresentation(photoImage, 1);
[photoData writeToFile:filePath atomically:YES];
How do I add EXIF data to photoData? Thanks

Cross platform UIImage NSImage in NSData format

I have an app on the mac and iPad that stores many images in the core data backed database. Previously they were images from URL's so there was no worries I just went from URL to data. But now I am creating thumbnails from the URL then saving it into the database. The issue I have is that I want to store them as NSData, but cant seem to find a format that I can store using the mac app, that the ipad version will be able to recognize and create an image from.
I tried,
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image.size.width, image.size.height)];
but then I get an error that Ipad does not recognize the data in DB and is incompatible.
anyone have ideas? or found a solution to this problem?
For the mac side I did
NSImage *smallImageGuy ....
NSData *pngRepOfImage = [self PNGRepresentationOfImage:smallImageGuy];
and on iOS side
UIImage *superDuperAwsomeImageGuy = [UIImage imageWithData:"NSData in DB"];
and TaDa!

Resources