How can I convert imageNamed to imageWithContentsOfFile? - ios

I tried to convert imageNamed: to imageWithContentsOfFile: and I failed everytime. I could not see pictures on cells.
UIImage *image = [UIImage imageNamed:[cellCountry objectForKey:#"Country_Flag"]];
cell.imageView.image = image;
[cellCountry objectForKey:#"Country_Flag"] holds flag1#2x.png
Path of image files is /Resources/Images
and This is my attempt.
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellCountry objectForKey:#"Country_Flag"] ofType:#"png"]];
cell.imageView.image = image;
How can I convert it? I am waiting your help...

Your attempt to use imageWithContentsOfFile is close. Since the value from [cellCountry objectForKey:#"Country_Flag"] already has the file extension, you can't also specify the extension when getting its path.
Try:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellCountry objectForKey:#"Country_Flag"] ofType:nil]];

You should try out the following
[UIImage imageWithContentsOfFile:[
[NSBundle mainBundle] pathForResource:#"mainScreenBackground" ofType:#"png"]]

Related

data type issue with image URL

I'm sure this is an easy fix, but I can't seem to work it out...
group[PF_GROUP_FEATURED] is just a database reference to a URL. I am trying to get these images to show up in my tableView cells.
My error is "incompatible pointer types sending UIImage to parameter type NSString.."
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:group[PF_GROUP_LOGO]]]];
cell.imageView.image = [UIImage imageNamed:image];
cell.imageView.image = image ;
cell.imageView.image = image;
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/occ/clm/UIImage/imageNamed:

My device can't show image array, but has no trouble finding the seperate images

In one of the views of my app, i want to display different animations made from a number of jpg images with the following code...
for (i = 0; i <= HighPic; i = i+1) {
[self.imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#%i", imageName, i] ofType:#"jpg"]]];
}
float imageCount = [[[self.selfContent objectAtIndex:overPath] valueForKey:#"lastImageNumber"] floatValue]*2-1;
imageView.animationImages = self.imageArray;
[imageView setAnimationRepeatCount:0];
imageView.animationDuration = (imageCount/25);
[imageView startAnimating];
This works fine inside the simulator, but when i run it on my device, the self.imageArray is equal null. If i instead write:
[imageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#0", imageName] ofType:#"jpg"]]];
It shows the right picture, even on the device, but just not the animations...
The reason i'm not using the
[self.imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%#%i", imageName, i]]];
Is because it takes up too much memory...
Thanks in advance!
I fixed the code myself. When i went to the .h file i saw the imageArray was set to weak so i replaced it with strong and that fixed it :)

how to convert a URL string into an UIImage [duplicate]

This question already has answers here:
iOS: load an image from url
(9 answers)
Closed 8 years ago.
NSString *imageName = [NSString stringWithFormat:#"%#UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
in imageName string getting the string as given below., How to convert these url to an image
http://appliicdev.xtechnologies.com/iapp/UploadedImages/5bc77bdd-3eaf-4491-813a-5aedcbfbba41.jpg
Thanks
You can use imageWithData from UIImage. Like,
UIImage *image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];
your coding is fine
change this line
NSString *imageName = [NSString stringWithFormat:#"%#UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
into bz ur URL contains /UploadedImages/
NSString *imageName = [NSString stringWithFormat:#"%#/UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
yourimageName.image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]];
will u need to load the UIImage in quickly` use
Asynchronous FreeLoader
SDWebImage
when I try your image in browser it showing the error so check once path is valid or not otherwise your image name is valid or not
Try this:
NSString *imageName = [NSString stringWithFormat:#"%#UploadedImages/%#",appDel.serverUrl,[[sCatArray objectAtIndex:indexPath.row] objectForKey:#"SubCategoryIcon"]];
UIImage *imgObj = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:imageName]]];
_yourImgView.image=imgObj;

UIImage with NSData initWithData is nil

The following code does not seem to load an image.
uiTabBarItem = [[UITabBarItem alloc] init];
NSData *datatmp = [NSData dataWithContentsOfFile:#"newsicon.png"];
UIImage *tmp = [[UIImage alloc] initWithData:datatmp];
uiTabBarItem.image = tmp;
datatmp is nil (0x000000) and
the image does exist.
I. Don't reinwent the wheel. Use tmp = [UIImage imageNamed:#"newsicon.png"]; instead.
II. NSData expects a full file path when being initialized from a file. The following would work (but you don't have to use this anyway, as I just pointed it out):
NSString *iconPath = [[NSBundle mainBundle] pathForResource:#"newsicon" ofType:#"png"];
NSData *datatmp = [NSData dataWithContentsOfFile:iconPath];
Loading an image from a file is best accomplished with:
[UIImage imageNamed: "newsicon.png"];

UIImage is empty after calling imageWithContentsOfFile

As you can see, that I have put my the national flags in a folder in Xcode and I am trying to display it to the navigation bar. However, it is not showing up and I found out:
NSString *imageName = [NSString stringWithFormat:#"%#.icns",countryName];
UIImage *image = [UIImage imageWithContentsOfFile:imageName];
image is "nil".
Any idea? Thanks!
You could use like this
NSString *imageName = [NSString stringWithFormat:#"%#.icns",countryName];
UIImage *image = [UIImage imageNamed:imageName];
Please Try This
Check whether the file actually exists. I suspect it doesn't. Use [NSFileManager defaultManager] fileExistsAtPath:.
Where was the image path you are sending NSString to here
UIImage imageWithContentsOfFile:imageName
send the path to that method. or make like this
UIImage *image = [UIImage imageNamed:[NSString stringwithFormat:#"%#.icns",countryName]];

Resources