imageWithcontentsOfFile failing to load saved image from simulator cache dir - ios

I'm writing an image to the simulators cache directory using
NSURL *cacheDir = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask].lastObject;
NSURL *avatarFileURL = [cacheDir URLByAppendingPathComponent:#"test.png" isDirectory:NO];
NSData *imageData = UIImagePNGRepresentation(avatarImage);
[imageData writeToURL:avatarFileURL atomically:YES];
This works fine and I've verified the image by logging the avatarFile url and loading it into a browser, which displays the cache image.
I then try and read the test image back and I'm getting different results if I use imageWithContentsOfFile: and imageWithData: as follows:
NSURL *cacheDir = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask].lastObject;
NSURL *avatarFileURL = [cacheDir URLByAppendingPathComponent:#"test.png" isDirectory:NO];
// Returns a nil.
NSString *avatarFile = avatarFileUrl.absoluteString;
UIImage *image1 = [UIImage imageWithContentsOfFile:avatarFile];
// Returns the image.
NSData *imageData = [NSData dataWithContentsOfURL:avatarFileUrl];
UIImage *image2 = [UIImage imageWithData:imageData];
I'm at a loss to explain why imageWithContentsOfFile fails and imageWithData works when they are both addressing the same URL. I've verified the URL by logging it and urls are exactly the same.
Anyone know why imageWithContentsOfFile is not working?

Your issue is with this line:
NSString *avatarFile = avatarFileUrl.absoluteString;
Look at the value of avatarFile. It will be something like file:///..... imageWithContentsOfFile: expects a proper file path, not a file URL.
You need to properly convert the file URL into a path. That is done as follows:
NSString *avatarFile = avatarFileUrl.path;

Related

image are not showing on imageview. in ios

http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1
I'm trying to download image on imageView. If you will paste this url on browser it will show, but on imageView it's not showing. If you will try any other image, then the same code will work, but when I used this url, the image does not show.
The image in question seems to be a WebP image (served with the wrong MIME type of image/png), which is not a format natively supported by UIImage. However, you can use iOS-WebP to decode the image:
Add the framework, header and implementation to your project, then use:
#import "UIImage+WebP.h"
NSURL *url = [NSURL URLWithString:#"http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1"];
NSData *data = [NSData dataWithContentsOfURL:url];
imageView.image = [UIImage imageWithWebPData:data];
And please remember to do the download and decoding steps asynchronously so as not to block the main UI.
Try this?
NSURL *url = [NSURL URLWithString:#"http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.img.image = [UIImage imageWithData:data];
Edit
The URL you provided is not an absolute path hence the data being fetched cannot be converted into an UIImage. There is something wrong with the URL or the formatting of it.

PNG image data has size 0 x 0 on iOS

I have a png file created in Pixelmator on OS X that I would like to use in my iOS app. This image displays fine in OSX, and when I create an NSData object with the URL of the file, the quicklook preview in the debugger also displays the image perfectly, but when I try to create a UIImage with the following
NSURL* imageURL = [[NSBundle mainBundle] URLForResource: name withExtension: #"png"];
NSData* data = [NSData dataWithContentsOfURL: imageURL]; //at this line, quicklooking "Data" shows the image I want
image = [UIImage imageWithData: data]; //this is the relevant line, where the failure is logged
if (!image) {
//this has the same result
NSURL* imageURL = [[NSBundle mainBundle] URLForResource: #"imageNotFound" withExtension: #"png"];
NSData* data = [NSData dataWithContentsOfURL: imageURL];
image = [UIImage imageWithData: data];
}
}
I get this error message
<Error>: CGImageCreateWithImageProvider: invalid image size: 0 x 0.
A final wrinkle; a similar image which prints this error displays perfectly when used as a texture with GLKTextureLoader.

View PDF file converted to BLOB image in UIImage or UIScroller

my App requirement is to view uploaded documents from IPAD.
i am importing document from database in the form of BLOB image and passing it to IPAD and with below code i could view image.
NSString *strImage; // String has BLOB image from DB
NSData *imageData = [self dataFromBase64EncodedString:strImage];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *m_oImageViewer = [[UIImageView alloc]init];
m_oImageViewer.image = image;
m_oImageViewer.frame=CGRectMake((self.view.frame.size.width/2)- 250,60,500,500);
[self.view addSubview:m_oImageViewer];
-(NSData *)dataFromBase64EncodedString:(NSString *)string{
if (string.length > 0)
{
NSString *data64URLString = [NSString stringWithFormat:#"data:;base64,%#", string];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data64URLString]];
return data;
}
return nil;
If PDF file converted to BLOB why i am getting image as nil, as i can see BLOB is common so why only .png files i can se and not PDF file even after converting to BLOB.
i read support document for UIImage it does not give PDF file but i am giving BLOB image. where i am going wrong.
i have to use any other property, or this is not possible.
PDF isn't image format, its something like container with text, fonts, vector and raster images etc, so you can't convert it into UIImage, but UIWebView can show pdf (and a lot another format). I think, you should save pdf BLOB to temp folder and then show it in UIWebView, something like:
[pdfData writeToFile:tempFilePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:tempFilePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

Random URL website address from core data that holds a picture to display in UIImage in iOS?

I'm new to using xcode, so any information or links would be helpful. If I had a RANDOM website url in coredata, how would I be able to direct my UIImage to get the photo from that website to display in iOS?
Let's say imageURL is a NSString * ivar or property where you hold your URL.
To get the image this should work:
NSURL *url = [NSURL URLWithString:imageURL];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *myImage = [UIImage imageWithData: imageData];
Of course you should add some error checking but this should get you into the right direction.

NSData not accepting escaped / UTF8 encoded URL

I am trying to create an NSData object that contains the contents of a file in my main bundle.
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL URLWithString:[chordPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
Prior to using stringByAddingPercentEscapesUsingEncoding I was getting a nil URL, so I went ahead and fixed that by reencoding the string. Now I get a valid URL but the chordImageData object is nil for me. The file is definitely included in my main bundle (since I was able to get the URL to begin with) so I am wondering what's wrong.
EDIT:
Running this:
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL options:NSDataReadingMapped error:&dataCreationError];
Gets me this for the error:
po dataCreationError
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7530a00
Looking around Google it seems that the URL is still not encoded correctly. Anyone know an additional step to make sure the encoding is valid?
Try using fileURLWithPath:, something like this:
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL fileURLWithPath: chordPath];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];
There should be no need to do percent escaping. The reason you're having problems is that you are using URLWithString: which is meant for "non-file" URLs.
You should use +fileURLWithPath: for file-based URLs:
NSString *chordPath = [[NSBundle mainBundle] pathForResource:chordName ofType:#"png"];
NSURL *chordURL = [NSURL fileURLWithPath:chordPath];
NSData *chordImageData = [NSData dataWithContentsOfURL:chordURL];
UIImage *chordImage = [UIImage imageWithData:chordImageData];

Resources