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.
Related
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;
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.
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];
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];
I had a lot of trouble trying to force the loading of the low-resolution version of some resources on iOS, when the high-res version (#2x) is also present.
The reason I wanted to do this is simple: My app shows a bunch of full-screen images to the user (320x480 on older devices, 640x960 on Retina devices), and the user can upload some of these to TwitPic. I wanted to unify the uploaded images to 320x480, regardless of the device (for consistency, because that size is fine in a web browser).
I found out that no matter what UIImage method I used, when the high-resolution version of a resource file is present it is loaded (even if you pass the full file path): UIImage will out-smart you. But I found a way to out-smart UIImage:
- (UIImage*) lowResImage{
NSString* path = [[NSBundle mainBundle] pathForResource:_fileName
ofType:#"png"
inDirectory:_resourceSubdirectory];
path = [path stringByReplacingOccurrencesOfString:#"##2x" withString:#""];
NSData* imageData = [[NSData alloc] initWithContentsOfFile:path];
UIImage* image = [[UIImage alloc] initWithData:imageData];
[imageData release];
return [image autorelease];
}
The above code works fine on a 4th generation iPod touch.
Anybody came up with a better approach?
Build your path, open data, and then init the image using data as in the example below.
NSString *dataPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:_fileName];
NSData *data = [[NSData alloc] initWithContentsOfFile:dataPath];
UIImage *image3 = [UIImage imageWithData:data];