Why [UIImage imageWithData:] supports PVRTC format? - ios

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?

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 would I go about saving snaps (UIImage) to the sandbox?

I'm creating a Theos tweak for my jailbroken iPhone and I'm stuck on a feature. So far I can block screenshot detection and replay detection, add infinite text, and have the app open directly to the feed. The feature I'm stuck on is saving incoming and outgoing snaps (and stories, but one step at a time). I used the FLEXible tweak from Cydia to assist me and I noticed that the snaps have something to do with "UIImage." From what I know I have to somehow convert this UIImage to *.jpg or *.png and save it somewhere (I'm looking to save it to the app's Documents directory).
I searched around the site and found the following that may be helpful:
// Convert UIImage to JPEG
NSData *imgData = UIImageJPEGRepresentation(image, 1); // 1 is compression quality
// Identify the home directory and file name
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.jpg"];
// Write the file. Choose YES atomically to enforce an all or none write. Use the NO flag if partially written files are okay which can occur in cases of corruption
[imgData writeToFile:jpgPath atomically:YES];
and
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.png"];
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
(like I said I found this; it is not my code).
Bah, I'm not sure how I would incorporate this. Any help?

How can I open an image from file system into UIImage View

Before anyone down votes my question, I have literally looked all over stack and cannot find the answer
I am making a phonegap app which I can place an image into my filesystem
the url :
file:///Users/danielnasello/Library/Application Support/iPhone Simulator/7.0.3/Applications/75EE3563-560D-4CFD-B357-313DD559573D/Documents/Vault/1386252707450.jpg
I can then pass this url to my modal controller to present an image in the image view.
the problem is I cannot seem to find the correct way to access the image from my filesystem and display it into my image view.
my current relevant code
(self.myImage) is the string i pass from phonegap. it does contain the url string because I am logging it, so I know the url is getting passed. However, the image simply will not display.
I tried using image named from one of my library images and it works fine. I just cant seem to find the correct way to present it using file url.
NSURL *url = [NSURL URLWithString:self.myImage];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
self.imageView.image= img;
here is the code for that.
NSString *filepath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"1386252707450.jpg"];
self.imageView.image=[UIImage imageWithContentsOfFile:filepath];
this code is actually correct. It just doesn't work on simulators (shocker), you need a real device

Displaying base64 encoded image in rails

Update:
I took the base64 string iOS was generating and decoded it to a binary file through a website. The image was encoded properly it seems.
My problem then is with the rails side of things. I'm displaying the image using the following code but it's just showing a broken image.
<td><%= ('<img src="data:image/jpg;base64,%s">' % Base64.encode64(user.imagestring)).html_safe %></td>
I'm trying to encode a base64 string to send through JSON to my web service (Rails using Postgresql). I've converted a UIImage to NSData and am now converting it to a base64 string.
I'm using the new base64EncodedStringWithOptions method. It converts to a long string but on my web service the image appears broken.
I uploaded the image to an image to base64 converter website and it returns a slightly different string.
Is the problem with the iOS encoder or am I doing something wrong?
UIImage *image = [UIImage imageNamed:#"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSString *blob = [imageData base64EncodedStringWithOptions:0];
You are doing a round-trip through UIImage and UIImagePNGRepresentation(image) to get the data to be base 64 coded. That might not be the same contents as the original file. There are tons of options when coding images, and you're not assured that your roundtrip will use the same options.
I would, instead, suggest just loading the NSData directly:
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:path];
Try running that through your base 64 coding and see what you get.
The problem was with the ruby code. This worked:
<td><%= ('<img src="data:image/png;base64,%s">' % user.image).html_safe %></td>

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];

Resources