Cross platform UIImage NSImage in NSData format - ios

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!

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?

How to get an image from server? or How to get NSData from NSUrl?

I am using xamarin.ios I have already uploaded a selected image to server. And I want to get it again. I am getting NSUrl as http://172.16.10.49/thunder_ex/backend/web/uploads/AppUserProfilePic/Profile_140.jpg . I want to use this NSUrl and show the image in UIImage. I tried
NSData data;
data = NSData.FromUrl(url);
profileImage.Image = UIImage.LoadFromData(data);
But I am getting data as null.
Your code should work, in fact it's very similar to the one I posted there.
If you get null it generally comes from either:
the URL, i.e. an invalid URL (that iOS does not like) will return `null; or
the downloaded data is not in a format that iOS supports.
The URL you provided does not seems to connect or load (from Chrome) for me. That could also be the reason (as the API does not have any other way to return an error condition).

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?

What OSX image format is compatible with 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];

Curtail UIImage download size from WEB

I have an application that downloads images based on URL using the call:
img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlToImage] options:0 error:&err]];
where URL is an image on the WEB.
I really want to curtail the amount of data transfer, so would like to reduce the file size of the download.
I was wondering if anyone has a magic way of reducing the file size of the download short of implementing a PHP server process to be the go between to take care of this.
Thanks in advance!

Resources