I tried to add a picture on an UIBarButtonItem like this :
I did it this way via XCode development interface builder. But, when I launch the application, the picture is blurred like this :
I tried different sizes for this "home" picture, but I have always the same problem with all pictures I tried (Not only this one).
Maybe I forgot something, or there is a "perfect size" I missed ?
Thanks in advance !
*********** EDIT :** Thanks to dariaa I notice I have to import 2 image : home.png and home#2x.png (For the retina compatibility) ********
How do you set the image for your UIBarButtonItem?
I believe if you use this method you should have no problems:
UIBarButtonItem *homeBarButtonItem = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStylePlain target:self action:#selector(yourSelector:)];
This way you do not have to worry about sizes.
Just make sure that you have retina version of the image. (you should have "home-25.png"-25x25 pixels image for non-retina devices (if necessary) and "home-25#2x.png"-50x50 pixels image for retina devices.
You can also create an "image set" in your xcassets directory. Import an image and then right-click and select "New Image Set". Once you do this, right-click again in the image box (the image preview on the right) and select iphone, iPad, etc. and fill in all the scaled resolutions of your image.
Xcode will automatically adapt the correct image to be used based on the screen resolution of the device
Related
Purpose
I am to make my iOS app run on the different device include iPhone and iPad.
I want to setup the ImageView's image with different size using the same name for Adapting to the different devices and landscape.
What I am doing
1.import image into Assets.xcassets for different device traits.
apple program reference: Create asset variations for different device traits
The image inside xcodes' Assets.xcassets has different width or height class setting. I name the image after "homeBackground"
The image like this :
[1]: https://i.stack.imgur.com/yXHk4.png
2. drag the imageView into Interface Builder
3. set the imageView's attributes: Image in the Interface Builder as the image's name "homeBackground"
4. view as iPad or iPhone
using xcode 8.3. I can preview the scene.
I can see the imageView's image is set successfully, When viewing as Ipad or iPhone
Problem
run the app in xcode. I don't see the imageView has the image in the simulator and I have the following question:
Could not load the "homeBackground" image referenced from a nib in the bundle with identifier "BTBubblePop"
complete
I run it in the iPad, the ImageView's image shows
I run it in the iPhone, the ImageView's image don't show.
thanks for your focus and comment.
the question is called by the image not set correctly.
The image's assets.xcassets change like this. That's worked.
The Image should like these.
Image should set like here
If I understand your question correctly the answer is that it goes by image set name.
Or in code
UIImage *image = [UIImage imageNamed:#"homeBackground"];
[[UIImageView alloc] initWithImage:image];
My tab bar icons appear blurry.
I created the icons using Photoshop, and followed the iOS Human Interface Guidelines when I decided the sizes of each icons.
e.g. icon size: 30x30px png
This only happens with the tab bar. I wonder if this happens because of the resolution of the images or because of programming issues...
You're using icon size 30x30 which I assume is for 1x (iPhone<4). Since iPhone>=4 needs 2x and 3x images so you have to include that also.
Either you use images with naming conventions like
star.png // 1x = 30x30
star#2x.png // 2x = 60x60
star#3x.png // 3x = 90x90
or you can use image.xcassets and put your 1x, 2x and 3x images there and use it.
Reference Xcode Assets Catalogs
Probably because using a device with the retina screen.
Try about changing the icon's filename to xxx#2x.xxx like "facebook_icon#2x.png".
Also, be aware that you can use a PDF file (vector graphics, resolution independent) instead of PNGs, and Xcode will render the appropriate resolutions at build time for you (I don't think you can easily do this for third party icons such as Facebook, but...).
To see how this is done, create a new project using the "Tabbed Application" template, and check the asset catalog for the tab bar icon images. It does just that for the circle and square icons of the "First" and "Second" initial tabs.
Use this line of code to set image for uitabbaritem in uitabbar .
tabbaritem.image = [[UIImage imageNamed:#“image”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
I have the following images in my Images.xcassets folder:
And I have my storyboard set up as follows:
Why am I getting these grey boxes instead of actual images? Note: the boxes show up in the simulator and the storyboard. Thanks.
Maybe it is because your images are only available in 1x scale but your simulator runs a retina device. Try a #2x.png version in your images.xcasstes.
I create a button which is look like this in interface builder
See the Login Button
But somehow when I run it on emulator or device the icon become smaller like this.
I don't know why it is smaller. I haven't done anything on that button in code.
How can I solve this?
Thanks.
Solved
Because 2x image and normal image of icon are the same size.
By double the size of 2x image. Then it is solved.
EDIT: Looks like the 2x image was NOT actually 2x. I feel dumb :(
I have created #2x images for my images, but when I run the app in iOS simulator using retina the images are blurry. See attached screenshot.
The url for the full image is:
http://i.stack.imgur.com/Dpk1k.png
Image of retina display is hardware device settings:
http://i.stack.imgur.com/Gyn2n.jpg
It looks like you are referencing the image in a UIImageView that is part of a scene in a storyboard. When you pull down the list of images to choose from do you see both the normal and the #2x versions listed? If you only see the normal version then the issue might be that the #2x image is not included in the target.
To make sure the image is in the target you should select the image in the project explorer (on the left side in Xcode) and then look in the utility panel (on the right side in Xcode) in the file inspector. Make sure the check box next to the target is checked in the "Target Membership" section.