Is there a way I can say for a particular UIImageView:
UIImageView *imageView = <#instantiate#>
imageView.image = #"someImageFromImageAsset";
and based on the device it's rendering on, it sets the right image. Obviously I need to set the image for a particular device but I am unsure how you do it.
iOS will automatically choose the correct sized image if you just use the name of the image. Use #2x in the image file name for retina display.
Objective-C
imageView.image = [UIImage imageNamed: #"someImageName"];
Swift
imageView.image = UIImage(named: "someImageName");
Related
Today when I search some information about #1x #2x #3x
something confuses me
are these only used as app icon? Because most messages always give me the example of the icon.
and If it can be used in app view, how is the code?
like this?
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"background.png"]];
Then, iOS will change #1x and #2x itself? OR I need to code like this:
#define deviceIsIPhone5 ([UIScreen mainScreen].applicationFrame.size.height == 568 ? YES : NO)
if (deviceIsIPhone5)
{
self.backgroundImage.image = [UIImage imageNamed:#"background#2x.png"];
}
else
{
self.backgroundImage.image = [UIImage imageNamed:#"background.png"];
}
You don't have to think about #2x #3x.iOS automatically handles it.
All you need to care about is to put right size image into the right box(.xcassets) if you want to show image properly in all iOS devices.
no need to care about 2x,3x and png.
just enter image name only. ios will automatically check image to use according to screen dimensions.
self.backgroundImage.image = [UIImage imageNamed:#"background"];
I display a retina image (with #2x.png extension) using:
myImage = [UIImage imageNamed:#"iPhoneBackground#2x.jpg"];
UIGraphicsBeginImageContext(myImage.size);
[myImage drawAtPoint: CGPointZero];
myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView = [[UIImageView alloc] initWithImage:myImage];
NSLog(#"Dimension:%f x %f",myImage.size.width,myImage.size.height);
[self.view addSubview:imageView];
However the image is displayed twice its size on the retina simulation. Image and simulator both have 640 x 960 resolution, so I would expect the image filling the screen.
I know there are other ways than CGContext to display an image, but that's the way I would need to other purposes in my code.
Any idea why I have this definition issue ?
Don't use #2x suffix
From apple documentation:
The UIImage class handles all of the work needed to load
high-resolution images into your app. When creating new image objects,
you use the same name to request both the standard and the
high-resolution versions of your image. For example, if you have two
image files, named Button.png and Button#2x.png, you would use the
following code to request your button image:
UIImage *anImage = [UIImage imageNamed:#"Button"];
You do not need to explicitly load a retina image, #2x will be automatically appended to the image name if the device has a retina display.
Change your UIImage code to: myImage = [UIImage imageNamed:#"iPhoneBackground.jpg"];
I use bg.png for iPhone, bg#2x for both iPhone retina and iPad, bg#4x for iPad retina.
Here is the code i wrote: (in Helper.m)
+ (UIImage *) imageNamed:(NSString *)name
{
name = [name stringByReplacingOccurrencesOfString:#".png" withString:#""];
UIImage *image;
if (IS_IPAD) {
if (IS_RETINA) {
image = [UIImage imageNamed:[NSString stringWithFormat:#"%##4x.png", name]];
if (image) {
return image;
}
}
return [UIImage imageNamed:[NSString stringWithFormat:#"%##2x.png", name]];
}
else {
if (IS_RETINA) {
image = [UIImage imageNamed:[NSString stringWithFormat:#"%##2x.png", name]];
if (image) {
return image;
}
}
return [UIImage imageNamed:name];
}
}
The file is correct, but the size of image is wrong.
If the file is automatically picked by the system (use [UIImage imageNamed:#"bg.png"]), then on iPhone retina, the size is still 320x480 (1 point = 4 pixels).
but if i use [Helper imageNamed:#"bg.png"], the size is 640x960. (1 point = 1 pixel)
So anyway to correct the size?
On a retina device
[UIImage imageNamed:#"bg.png"]
searches for bg#2x.png first. If that image exists, it is loaded and the scale property
of the image is automatically set to 2.0. On the other hand,
[UIImage imageNamed:#"bg#2x.png"]
also loads that image, but uses the default scale = 1.0.
If you have to use a custom loading mechanism, you have to adjust the scale factor.
Since scale is a read-only property of UIImage it cannot be set directly. One method
I know of is
UIImage *tmpImage = [UIImage imageNamed:#"bg#2x.png"];
UIImage *properlyScaledImage = [UIImage imageWithCGImage:[tmpImage CGImage]
scale:2.0
orientation:UIImageOrientationUp];
Why reinvent what Apple already provides? The imageNamed: method already supports the ability to load iPhone or iPad specific images by using the ~iphone and ~ipad suffixes.
To get an iPad specific retina image you just name it bg#2x~ipad.png.
To get an iPad specific non-retina image you name it bg~ipad.png.
The problem you have with your code is due to the non-standard naming conventions, the image is loaded with the wrong scale.
Update: based on additional information in the comments, a better approach to solve this issue is to replace the use of the UIImage imageNamed: method with calls to the UIImage imageWithData:scale: method. This provides better memory management as well as the ability to specify the proper scale for the custom naming conventions and image sharing between iPhone retina and iPad non-retina.
![enter image description here][1]I am taking a UIImage from UIImagePickerController and showing it in a UIImageView. Actually the UIImagePickerController is in full screen and thus the UIImage is also. But UIImageView is not in full screen because of presence of a header bar.
The Image is getting stretched..
How do I fix this..
Please help me someone..
THANKS IN ADVANCE.....
First image is the image picker screen and second one is where I am displaying the UIImage captured from picker in a UIImageView.
![This is my UIImagePickerController][2]
![This is the image after capturing being shown in a UIImageview. ][3]
NOTE: BOTH THE SCREENS ARE IN LANDSCAPE MODE
Use this for your UIImageView
imageView.contentMode = UIViewContentModeScaleAspectFill;
You won't get any space and with scale preserved. However, some part of the image will be clipped off.
If you use:
imageView.contentMode = UIViewContentModeScaleAspectFit;
There will be some empty space, but scale is preserved.
I have a high resolution image and I want to use it as a background for a view. But when I add it either via an Interface Builder or programatically I see only its part.This doesn't help:
UIImage* _backGround = [UIImage imageNamed:#"background-clean#2x.png"];
CGRect _viewFrame = self.view.frame;
[_backGround drawInRect:_viewFrame];
UIImageView* _backGroundView = [[UIImageView alloc] initWithImage:_backGround];
[self.view addSubview:_backGroundView];
[self.view sendSubviewToBack:_backGroundView];
And this too:
_backGroundView.contentMode =UIViewContentModeScaleAspectFit;
So the question how can I scale this image in order it fits in the view in a full size in spite of its size?
P.S. Sorry for my bad English.
I agree with Ali, when creating retina-sized images you should also scale them and add their smaller version to your project. With both versions you can simply specify the full name of the image without the #2x.png or .png extension.
But, to fix your implementation you only need one extra line of code:
UIImage* _backGround = [UIImage imageNamed:#"background-clean"];
UIImageView* _backGroundView = [[UIImageView alloc] initWithImage:_backGround];
_backGroundView.frame = self.view.frame;
[self.view addSubview:_backGroundView];
[self.view sendSubviewToBack:_backGroundView];
Once you change the frame of the UIImageView, its contents (i.e. your image) will scale as well.
You must not include #2x in your code when referencing the name of the image!
You should:
Include both files Background.png and Background#2x.png in your Xcode projet resources, Background#2x.png being twice the size (in pixels) of Background.png
But always refer to this file (for example in imageNamed:) as #"Background.png" or even just #"Background": [UIImage imageNamed:#"Background"]
Then iOS will do its magic all by itself, choosing the right image between Background.png or Background#2x.png depending on if the device screen is Retina or not.
For more info see here in Apple documentation.
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background-clean"]]];