I have an image with the following naming convention.
It always shows the non 2x version on retina device.
I had removed cache images from derived data but still not showing.
It works if i explicitly set the imageNamed to "Back"
These are the images.
Back#2x.png
Back.png
UIImage *backImage = [UIImage imageNamed:#"Back"];
NSLog(#"back image height %f",backImage.size.height);
NSLog(#"back image width %f",backImage.size.width);
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setImage:backImage forState:UIControlStateNormal];
btnBack.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[btnBack addTarget:self action:#selector(Click_On_Btn_Back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = backBarButton;
Try
[UIImage imageNamed:#"Back.png"];
Also, make sure both versions of the image are listed under Target->Build Phases->Copy Bundle Resources. If the #2x version is not, make sure when you import it you check the box "Copy into destination group's folder (if needed)."
You should not specify .png in imageNamed: parameter.
Example: files should be names Back.png and Back#2x.png; Your code should be
UIImage *backImage = [UIImage imageNamed:#"Back"];
This is just one approach, but you can get the path from the bundle:
// in this example, we load test.png from the bundle
NSString *pathToResource = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"png"];
What makes this so convenient is that it not only automatically determines whether to use the #2x version of your image, but also when you have localized files, this provides the path the the file for the current user locale. This is handy since localized files are not in the main directory, but are rather in their own subfolders (for example, English localized files are in the #"en.lproj" subfolder), and calling them by name is a hassle because you need the full path. This method gets the path for you.
#Keller's answer should work. are you sure that you are using png images.
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];
Hello totally new iPhone development
I opened an empty project
I'm trying to add a picture to viewcontroler
Using this code,
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"a_image.png"];
imgView.contentMode = UIViewContentModeCenter
What path in the project a_image.png should be placed
I tried to put the image in the project
But the code does not know to take the image from there
What else should I do for my picture displayed
you can put a .png image in resource folder or any folder inside the project like as Supporting file and also you can also make a unique folder for images
check below in the image you can you different .png files are in the supporting file
I am facing memory issue when adding a UIButton to a UITableView. Below is my code for setting the UIButton's image:
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
But when I use imageName: method instead of imageWithContentsOfFile: method it's working perfectly. Does anybody have good solution for this issue?
imageWithContentsOfFile: vs imageNamed:
As per my knowledge,
imageNamed loads the image in a special system cache, and then future calls with that image path will return the image in the cache instead of reloading it from disk.
imageWithContentsOfFile simply loads the image at the path you specify, but does no caching. Multiple calls to imageWithContentsOfFile for the same image will result in multiple copies in memory.
AND about memory leaks i am not sure which one is better from both to use while using large number of images in programming...
I have an NSBox set up in my main view that accepts drag and drop. We store the URL into str. We then load the image and add it to the content view of NSBox.
imageView = [[NSImageView alloc] initWithFrame:CGRectMake(0, 0, 390, 150)];
NSURL *imageURL = [NSURL URLWithString:str];
NSImage *image = [[NSImage alloc] initByReferencingURL:imageURL];
[imageView setImage:image];
[self setContentView:imageView];
However, this doesn't do anything. The image is not displayed in the nsbox.
Another oddity. At first I was trying to add the NSImageView via interface builder, but the only image container they had was IKImageView... Is this odd? Isn't NSImageView more ubiquitous? I mainly develop on iOS, so I have the version from the ios developer site.
Any thoughts?
Edit: I should also add, when I was using IKImageView in IB, the image would show up.
Edit2: I've also tried just taking the initWithFrame out and replacing it with just init, no go.
The problem was that I was using initByReferencingURL to get the NSImage. This doesn't work for local files, so instead I used initByReferencingFile and everything worked out well!
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.