I've been stuck with adding a UIImage to my iPhone application, to add an image i usually used the name of the image and put it like that :
UIImage* myImage1 = [UIImage imageNamed:#"myImage.png"];
But in my application i don't know the name of the image when i'm writting the code, i've got the image name with a webservice and i put it in a NSString.
So i have a string m_image1, and i want to display the image in a UIImage like that :
UIImage* myImage1 = [UIImage imageNamed:m_image1];
But nothing appears on the screen, when i use NSLog to show if m_image1 has the right string it works, and when i paste what is print in the log in the imageNamed it works.
I've already tried using the path of my application, but nothing do it ! So i don't know what to try anymore !
That's my all block of code :
NSLog(#"m_image1 : %#",m_image1);
UIImage* myImage1 = [UIImage imageNamed:m_image1];
m_result1ImageView = [[UIImageView alloc] initWithImage:myImage1];
m_result1ImageView.frame = CGRectMake(0.f, 0.f, 163.f, 260.f);
[m_ScrollImages addSubview:m_result1ImageView];
Everything work just fine, when i put :
UIImage* myImage1 = [UIImage imageNamed:#"myImage.png"];
instead of :
UIImage* myImage1 = [UIImage imageNamed:m_image1];
it display the image !
I think your image is not in the resources!
Check your image: Go to the Project Navigator and click on blue icon which is your project. Now in Targets click on your target and go to Build phases tab and check Copy bundle resources.
Your image file must be in the list, if it is not add it and check the program again!
I hope it be useful for you!
try this code ,
m_result1ImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: m_image1]]];
I think you have not copied exactly the images in your application bundle that is being not displayed .
Try & check removing bunch of images and add those again in your project.
Related
I have a very weird issue. I have a watch app and I am trying to display an image programatically. When I debug the code, I can see the image is correctly loaded but on simulator or real device I cannot see the image. I have added that image in both watch and extension bundle resources.
Firstly I use the code below to open the view.
NSArray *objects =[NSArray arrayWithObjects:#"MainPageView",#"InterfaceController",#"PoseImageView",nil];
[self presentControllerWithNames:objects contexts:nil];
Inside PoseImageView I am trying to display an image with:
UIImage *imgTest = [UIImage imageNamed:[NSString stringWithFormat:#"1.jpg", strAngle]]; // I can see 1.jpg in debugger
[_imgPose setImage: imgTest];
The image is not there on watch or simulator. I am wondering how to fix the issue.
Note: From assets its not loading as well.
Note2: I can only display the image if the view is initial view.
You are trying to use imageNamed for .jpg which will not work.
Try using
NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"imageName"] ofType:#"jpg"];
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
I'm trying to add a UIBarButtonItem containing a UIImage to a UIToolbar. The image keeps being tinted and I can't get it to show as the original colored image - all I want to do is display an image, verbatim, in a UIBarButtonItem! I'm following the directions in the iOS 7 transition guide to set the image rendering mode to UIImageRenderingModeAlwaysOriginal.
UIImage *image = [UIImage imageNamed:#"myImage.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *ratingImage = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:ratingImage, nil] animated:YES];
One thing to note is that I set the tintColor for the main UIWindow of my app right when it loads...maybe this isn't important with regard to my issue, but thought I'd mention it.
I spent an evening trying to figure this out as well. You were very close to the solution.
The trick is to instantiate the UIImage with the rendering mode.
Instead of doing:
UIImage *image = [UIImage imageNamed:#"myImage.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
do this:
UIImage *image = [[UIImage imageNamed:#"myImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
and it works!
In my case, I had dragged a Navigation bar to my viewcontroller in the IB, and added the BarButtonItem. But don't provide the item an image in the IB. Make an outlet and assign it the UIImage (like we created above) by doing this:
[myCustomBarButtonItem setImage:image];
Hope this works for you.
UIImageRenderingModeAlwaysOriginal can also be set by selecting the image in your Assets.xcassets "folder" in XCode and setting the "Render as" dropdown to "Original image".
For Swift 2.1+ it would look like this:
let image : UIImage? = UIImage(named:"myImage.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
UPDATED Swift 3
let image : UIImage? = UIImage(named:"myImage.png")!.withRenderingMode(.alwaysOriginal)
The accepted answer is fine but if you placed the UIBarButtonItem in a storyboard or xib then you can just:
Go to the Assets catalog where the image lives
Select the image
Go to the attributes inspector (cmd-opt-4)
Set "Render As" to "Original Image"
Only do this if you want all instances of this image to show up without tinting.
If you want it to work for versions of iOS less than v7, you might need to to this:
UIImage *image = [UIImage imageNamed:#"myImage.png"];
#try {
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} #catch (NSException *exception) {
}
Since imageWithRenderingMode: is an iOS 7 method, you'll get an exception if you try and use it with a lesser version.
I'm doing an application using NSXmlParser. I'm getting data from server.I have to display details of the item including item image also.
I'm getting image data from parser, which is not adding to imageView .I'm using code as
UIImageView *warrantyCardImage;
warantyCardImage.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:warObj1.warrantyCardImagePath]]];
When I'm printing in console I'm getting value .By giving code as
printf("\n%s:",[warObj1.warrantyCardImagePath UTF8String]) and getting output as
http://123.237.186.221:8080/Warrantify/Barcode/barcodefbbab1dd-9ff0-448b-8d46-dcebf5d11b37.png
If I'm giving code as
UIImageView *warrantyCardImage;
warantyCardImage.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://123.237.186.221:8080/Warrantify/Barcode/barcodefbbab1dd-9ff0-448b-8d46-dcebf5d11b37.png"]]];
it is working ,the image is adding to imageView
Guy's hope that I will get a quick response from your side.
Thanks to all,
Sri Lakshmi.
Verify that your data object is really being formed correctly. You are assuming it is in your sample.
UIImageView *warrantyCardImage;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://123.237.186.221:8080/Warrantify/Barcode/barcodefbbab1dd-9ff0-448b-8d46-dcebf5d11b37.png"]];
if(imageData)
{
warantyCardImage.image=[UIImage imageWithData:imageData];
}
else
{
NSLog(#"sadness");
}
You are using a synchronous download method which maybe isnt the best. Explore the docs around NSURLDownload http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLDownload.html which might serve you better.
is it possible to assign a highres custom uitabbaritem image?
UIImage *img;
img = [UIImage imageNamed:#"TabIcon51#2x.png"];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"more" image:img tag:5];
this doesnt work. is there a workaround, or even better an officel link / solution for this usecase?
thanks
alex
If you're using imageNamed, you can exclude the #2x. The way imageNamed works is that if you're on a high res device it automatically loads the #2x file if it exists, otherwise it loads the regular file.
I Use the following code to load png image:
UIImage *imageBack1 = [UIImage imageNamed:#"Bar1.png"];
UIImage *imageBack2 = [UIImage imageNamed:#"Bar2.png"];
imageBack1 work right when imageBack2's value is nil, Bar1.png and Bar2.png are located at the same place,but why Bar2.png couldn't be load?
In fact , the problem come from the png file,but not cocoa. Iphoto couldn't recognize it.