Getting nil image even though the file exists in directory - ios

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?

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?

iOS - write jpeg file to file system

I'm new to iOS development and I'm trying to write an image as a jpeg file to the file system. From the logs I know that those file are indeed written to the file system, but when I try to save them to the camera roll they all appear black. I'm using the following code to write them as jpeg files:
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpegPath atomically:YES];
And the following code to write to camera roll:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Anybody know how to verify that those jpeg files are indeed written to the file system? And what I might be doing wrong in the second line of code?
EDIT: So here is the entire method:
- (BOOL)createImagesForSlides:(NSString *)documentsPath destinationURL:(NSURL *)destinationURL
{
NSFileManager *manager = [NSFileManager defaultManager];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:destinationURL];
NSString *folderName = [NSString stringWithFormat:#"/%#", document.title];
// create new folder
NSString *newFolderPath = [documentsPath stringByAppendingString:folderName];
BOOL result = [manager createDirectoryAtPath:newFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
// create a jpeg file for each page of the pdf file
for (int i = 1; i <= document.numberOfPages; ++i) {
NSString *jpegPath = [NSString stringWithFormat:#"%#/%d.jpg", newFolderPath, i];
[UIImageJPEGRepresentation([[document pageForPageNumber:i] image], 1.0) writeToFile:jpegPath atomically:YES];
UIImageWriteToSavedPhotosAlbum([[document pageForPageNumber:i] image], nil, nil, nil);
}
return result;
}
document is a pointer to a CPDFDocument instance, and it's from some open source reader code available on github (iOS-PDF-Reader). What I basically do here is grab each page of the pdf document, generate an image, and then save them to the file system as jpeg file. Weird enough, even though there are more than 10 pages in the document, UIImageWriteToSavedPhotosAlbum only writes 5 files to camera roll. Any idea why?
It would probably be useful to see how you construct jpegPath here.
First, writeToFile:atomically: returns a BOOL, so check that for your first indication of success or failure.
There are a couple of ways you can verify that the image is written to the file system. If you are running on a device use something like iExplorer to access the file system and look at the file written. Since it is NSData* you can cheek the file size to make sure it looks reasonable. On the simulator, dig into the folder structure under ~/Library/Application Support/iPhone Simulator/ and examine the file. Without looking into the filesystem itself try reading the image back into another UIImage (imageWithData: in your case since you are writing a NSData* object).
There doesn't appear to be anything wrong with your UIImageWriteToSavedPhotosAlbum call according to the docs. It is OK for the last 3 arguments to be nil (all are marked as optional), you just have to be sure the UIImage is valid. Have you set a breakpoint to be sure you have a valid image (Xcode Quick Look feature)?

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

iOS Caching images with AFImageCache doesn't seem to work

I'm trying to cache some images loaded from a URL by using AFImageCache, without success... I'm not sure If i'm doing anything wrong and would love your opinions if you've ever used this library.
I'm getting an image from a NSURLRequest, and then caching it as follows
UIImage *img; //This image is a png icon returned from the server
NSData *imgData = UIImagePNGRepresentation(img); // Always checking length to make sure its more than 0, and it is
[[AFImageCache sharedImageCache] cacheImageData: imgData
forURL: myURL
cacheName: #"IconCache"];
On the next run, i'll try to check if the image was cached, but will always get nil for some reason.
UIImage *myIcon = [[AFImageCache sharedImageCache] cachedImageForURL: myURL cacheName: #"IconCache"];
NSLog("%#", myIcon); // output (null)
Any ideas or thoughts about this?
Thanks.
What version of AFNetworking are you using? AFImageCache was removed from the public API a long time ago. If you want to do image cacheing, use NSURLCache, which caches all network responses on a system level. AFImageCache has always just been a temporary cache used to improve the performance of image loading in scroll views.

Compare two UIImages with CC_MD5 in iOS ,but the hash code will be changed after reboot the device

I 've use
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(inImage)];
CC_MD5(imageData, [imageData length], result);
for generate MD5 code of my picture and add them to NSDictionary for compare the image from photoLibrary.
But when I add some code or reboot my device , I find the hash code is totally changed .
How can resolve this problem ?
You should use
NSData *imageData = [NSData dataWithContentsOfFile:file];
CC_MD5(imageData, [imageData length], result);
that must work fine.
Also consider to use sha1 as basing algorithm.
PNG file format allows to have timestamps inside the binary data. This will be different every time you call UIImagePNGRepresentation. I am afraid you can't use MD5 to compare the data of two images this way. Try to extract the raw image data from the files and generate MD5 of the raw data.

Resources