set retina image in uiimageview - objective-c

today i first run my app on my iphone but all my images (my background image too) were blurry, i read in the apple documentation and i understand that i should use a double pixel size (640X960 instead of 320X460). to set my image background i just use an uiiamgeview and set the image in the xib file. now, my question is how can i set the image from the .m file and not from my xib file. when i try to set the image from the .m file my image was much bigger then my uiimageview (i see only part from the image)?
thanks!

When using retina double sized images you don't actually have to do anything as far as loading them in code or in Interface Builder.
You just need to name the properly and make sure they're in your Xcode project.
For example if you had one image.png you'd need an image#2x.png in your Xcode project as well. iOS will automatically use the correct when when displaying on a Retina Device.
Update: In response to your comment.
This will create an UIImageView and set it to the size of the view. Its very similar to how most people do it in Interface Builder.
- (void)viewDidLoad
{
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"];
[background setFrame:CGRect(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:background];
}

Related

How to stretch Image in OSX app?

I want to stretch an Image to fit a view. In iOS I have used this function.
[myImage stretchableImageWithLeftCapWidth:12 topCapHeight:13];
This stretches the image correctly and doesn't distort its edges and corners. In OSX app development I am using NSImage and NSImageView. Is there any OSX equivalent way of achieving this? If it can be achieved through storyBoards then it will be wonderful.
You could simply set the imageScaling. Apples Documentation gives you the following options;
NSImageScaleProportionallyDown
NSImageScaleAxesIndependently
NSImageScaleNone
NSImageScaleProportionallyUpOrDown
If you just want to stretch the image to fit the NSImageView you can do something like this.
Swift
let imageView = ...
imageView.imageScaling = .ImageScaleProportionallyUpOrDown
Objective-C
NSImageView *imageView = ...;
[imageView setImageScaling: NSImageScaleProportionallyUpOrDown];

How do I programmatically change an image in an existing UIImageView and make it fit inside the image view's constraints?

I have a UIImageView and when I set an image in that view using IB, the image is the correct size when I run the app (40x40). When I programmatically change the image using the code below, the new image is 400x400 or so. This is using the same image that I know works fine if it's set in IB. Do I have to scale the image before I add it to the UIImageView? I assumed auto-layout's constraints would automatically do this for me.
UIImage *messageTypeImage = [UIImage imageNamed:message.imageName];
[messageCell.imageView setImage:messageTypeImage];
[messageCell.contentView layoutIfNeeded];
The UIImageView's content mode is set to "Scale To Fill" and I've tried adding the code below both before and after calling setImage:
[messageCell.imageView setContentMode:UIViewContentModeScaleAspectFill];
[messageCell.imageView setClipsToBounds:YES];
Here's how the constraints are set up for the image view:
Are you using iOS 8 SDK? If so, try overriding layoutSubviews in your cell subclass and add this:
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
There are some problems with the contentViews' of collection view/table view cells constraints/masks.
Wow. Finally found the answer here. Turns out, there's a default imageView outlet in prototype cells. I was unknowingly using that outlet and not the UIImageView I thought I was using.

Usage of retina images and setting an UIImageView

It is the first time that I need to use images with the name formats of like: #2x and -568h#2x But I do not understand when the -568#2x is used. This should be for iPhone 5 and up but, the image is not used on my iPhone 5S when testing.
I tried very hard searching for this problem, but I cannot find it.
Problem:
Only the #2x image is used on all devices, and the -568#2x is ignored totally. So there is a gap above my image on screen while I have set the UIImageView in Storyboard to fill the whole screen. So in my understanding, with the setImage it would look first in the Project folder, and then to the Asset Catalog. But still it does not take the -568#2x image and always take the #2x image.
I have also set the auto layout right for the UIImageView so it would fit top, bottom, left, right.
Code/Screen:
So I have 2 images in my Project folder as you can see on the image:
Link to image
Here is the gap on top:
Link to image
And then in my code I use this code for setting the image on my Outlet:
[[self image] setImage:[UIImage imageNamed:#"firstLaunch"]];
So what could be the problem? Am I totally wrong how the Automatic image names thingy is working?
Thanks!
You could do something like this:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
self.view.layer.contents = (id)[UIImage imageNamed:#"firstLaunch#R4"].CGImage;
} else {
self.view.layer.contents = (id)[UIImage imageNamed:#"firstLaunch#2x"].CGImage;
}

Change background image on all scenes iOS 7

The storyboard looks like the image below. In each scene I had to drag an UIImage object from the object library, size it to fit between the nav and tab bars then set its image to that baige blur with the attributes inspecter.
I'm curious, is there a way to do it in one go to all scenes from the appdelegate.m file? thanks
Try this in your AppDelegate:
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"whatever.jpg"]]];

do I need to initialize the new view?

I admit I'm a newby to Objective-C but came pretty far, I guess. Now I'm having an issue and would need some help.
In my app I display images in portrait modes amongst other information and want to display them full screen in landscape mode. I learned that you'd do this defining a Landscape m, h and nib file and load this view when the device is moved to landscape orientation (working with an observer), right?
In portrait mode the images in the end depend on user interaction so the Landscape class needs to be told, which image to display. I created a putImage method in the Landscape class
-(void) putImage:(NSString *)theImage {
pict.image = [UIImage imageNamed: theImage];
NSLog(#"PICT %#",pict.image);
NSLog(#"IMAGE %#",theImage);
}
and call it from the portrait one and here comes the issue:
NSString *actualImage = [NSString stringWithFormat:#"Picture%i.jpg",selected];
pict.image = [UIImage imageNamed: actualImage];
[landscapeViewController putImage:actualImage];
that works perfectly every time except the very first one. So whatever I do I need to first make the App load a new image plus change to landscape orientation (being displayed no image) in any manner. Then after it always works displaying the correct images.
I added debug information and see that the correct image is assigned to pict in landscape class - however it seems it needs to be displayed before it really sets the image.
Any help you guys could give?
Not sure if this it it since your example code is pretty sparse. But when you load your first picture do you do that in init? If so, try to do it in viewWillAppear or viewDidAppear.
thank you for all the hints and tries to help. Now I found the solution myself ... here's what I did:
1) in the LandscapeViewController.h I defined an NSString:
#interface LandscapeViewController : UIViewController
{
IBOutlet UIImageView *pict;
NSString *actualImage;
}
2) I changed the putImage method to only assign the image name to my local NSSTRING variable:
-(void) putImage:(NSString *)theImage {
actualImage = theImage;
[actualImage retain];
}
3) loading the image I only do in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
pict.image = [UIImage imageNamed: actualImage];
[super viewWillAppear:animated];
}
So the answer to my question is almost answered - I guess that this issue arose because I did set the image before I loaded the view first time so that it was not instanciated. I was able to call the method but had no Image Object to set ... does that sound logical to you guys?
Thank you all again!