Since the last Xcode update I can't work with images - ios

This is the basic code I use to add an image named "Background.png" to an UIImageView:
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:#"Background.png"];
imageView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:imageView];
Since the last Xcode update the resource can't be loaded.
I tried running on an iPhone 6 - iOS 10.3 and the Simulator - iOS 11.0
For some reason even though the iOS deployment target is version 10.3 the simulator runs on iOS 11.0
The image is there, in the same folder as the project named: "Background.png"
I've been doing the same thing for years and now it has stoped working.
Anyone had a similar issue?

You may want to utilize the purpose of Assets.xcassets. Put your assets there and you can easily use your images. Note that you do not have to put the image's file extension. It's easy, like so:
UIImage *markerImage = [UIImage imageNamed:#"marker"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:markerImage];
[imageView setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:imageView];
I have an image in my Assets.xcassets named marker.
Edit: this is tested, I made an Objective-c project just for this question :)

I had the same problem and i solved it as follow:
Simply Select your image, then check the box linked to your projet name in "Target Membership" (on the Right Window)
is it working ?
There is a Screenshot if needed:
https://i.stack.imgur.com/gTOGJ.png

Related

Image is showing on simulator but not on a real iPhone

So I got this piece of code, I want to show a picture on my view:
imgLogo = [[UIImageView alloc] init];
[imgLogo setFrame:CGRectMake(60, 200, 200, 200)];
[imgLogo setImage:[UIImage imageNamed:#"MyFrigginAwesomeLogo"]];
[self.view addSubview:imgLogo];
But the problem is that it works correctly when testing it on the simulator(iOS 7) but it doesn't show anything on my iPhone(4S with iOS7) itself.
Answer added from Comment:
The iOS devices are case sensitive, but the simulator is not. Make sure you are trying to load the exact filename in order for them to be found.
Try using the designated initializer for UIImageView instead of just init:
NSString *logoName = #"MyFriggingAwesomeLogo";
imgLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:logoName]];
imgLogo.frame = CGRectOffset(imgLogo.frame, 60, 200);
[self.view addSubview:imgLogo];
It's also possible that you've added the image resource to only the simulator target. In your bundle settings Build Phases, make sure you have the image listed in Copy Bundle Resources.

Image missing from distribution

I am having an issue where an image is missing, but only when built for distribution. Also, it only occurs on the iPhone 5, but not for the iPhone 4 or 4S.
This is a screenshot when built for development, on the simulator.
And this is a screenshot when built for distribution, on an actual iPhone 5.
I have a single target for both development and distribution. Relevant code:
int deviceNumber = 4;
if([UIScreen mainScreen].bounds.size.height > 500)
deviceNumber = 5;
MYIntroductionView *introductionView = [[MYIntroductionView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height) panels:#[panel1,panel2,panel3]];
if(deviceNumber == 4){
[introductionView setBackgroundImage:[UIImage imageNamed:#"One Degree_Iphone4_Background.png"]];
}
else{
[introductionView setBackgroundImage:[UIImage imageNamed:#"One Degree_Iphone5_Background.png"]];
}
And within MyIntroductionView.m, the relevant code is:
-(void)setBackgroundImage:(UIImage *)backgroundImage{
self.BackgroundImageView.image = backgroundImage;
}
-(void)buildBackgroundImage{
self.BackgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
self.BackgroundImageView.backgroundColor = [UIColor clearColor];
self.BackgroundImageView.contentMode = UIViewContentModeScaleToFill;
self.BackgroundImageView.autoresizesSubviews = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self addSubview:self.BackgroundImageView];
}
buildBackgroundImage is called within the init function.
I'm not 100% sure this is still the case but I know it used to be:
The iOS Simulator is not case-sensitive with regard to external filenames (such as your images). So if the image is named "myImage.png" and your program asks for "MyImage.png", the simulator will correctly find and load the image.
However when you try and run it on the device, it is case sensitive, and will not accept the incorrectly-cased file.
So double check that your capitalization of the file itself is identical to what you're asking for in your program. Hopefully that will fix your issue right up.

How to add large background image on the UIView in a full size?

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"]]];

UIImageView image not showing up

_topBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)];
UIImageView *bar = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"top_bar.png"]];
bar.frame = CGRectMake(0, 0, 320, 45);
[_topBar addSubview:bar];
_up = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"up.png"]];
[_topBar addSubview:_up];
_up does not show up in my build. If i exchange the "up.png" for any other image in my resources folder, it shows up, but up.png refuses to. Is there anything I can do? I've initialized it using just a UIImage, a statically allocated UIImageView, removed and re-added the .png files, took them out of the resources folder and put them somewhere else, re-saved them, everything aside from throwing them out.
EDIT:
The file was saved as up.png but was really up.png.psd. just a matter of reading the fine print.
Check the Kind in Finder. Its probably not a png and therefore wouldn't display.
I now know this to be true since you commented :)
A couple of things. Select the file in the right pane, open the rightmost (3rd) file inspector pane, and insure that this image is included in your Target.
In code, break out this:
UIImage *image = [UIImage imageNamed:#"up.png"];
NSLog(#"image %#", image);
_up = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"up.png"]];
NSLog(#"UP FRAME: %#", NSStringFromCGRect(_up.frame) );
// maybe set the frame to what you want?
_up.frame = (CGRect){ {0,0}, image.size }; // set origin where you want it

Can not load UIImage on button

I want to add UIImage on button,but the del.png can't show on my button,
What can i do?
I try many load Image Method,but no effect,such as:
UIImage* delImage = [[UIImage alloc] initWithContentsOfFile:#"del.png"];
UIImage* delImage = [UIImage imageNamed:#"del.png"];
UIImage* delImage =[UIImage imageWithContentsOfFile:#"del.png"];
CalDelBtn *BookItem = [[CalDelBtn alloc] initWithFrame:CGRectMake(startPointX, startPointY, 75, 113) statuePicture:delImage];
Please give me some advice,thank you!!
First, you image could be loaded with
UIImage * image = [UIImage imageNamed:#"del.png"];
This is the simplest way to load an image, so it should work with an autoreleased image.
Don't forget to add it to your xCode projet explorer! Here's a list of reasons for what your image generally woudn't display : https://stackoverflow.com/a/9698764/127493.
Then, add it to your button with :
[button setBackgroundImage:image forState:UIControlStateNormal];
for example. Does it work ?
Can you verify that your image is being added to your target? Xcode 4.3 has a nasty habit of not including files into targets when you drag and drop. So click on your project file on the left side of Xcode, go to Build Phases and see if your image is in the list under Copy Bundle Resources.

Resources