UIImageView image not showing up - ios

_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

Related

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

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

UIImage retutns wrong width

I have a UIImageView and I put it in a toolbar. When I run it on the simulator, the image gets stretched in. Meaning the width somehow gets less than the toolBar. Here's my code:
UIImageView *imageNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
nav.frame.size.width, nav.frame.size.height)];
nav.image = [UIImage imageNamed:[imageTop objectAtIndex:0]];
This is the output.
http://postimg.org/image/rjirnmiyx/
The yellow is the toolbar and the green thing is the image. It does not fill the toolbar.
Update
Whatever I do, it gets scaled off at the right side. (Like the image above.)
I tried all kinds of content modes and it all came down to one thing. Something does get ruined at the right hand side.
Update 2
I figured out the problem! The problem is that the UIImageView does not have a constraint. Can someone please help me add a constraint?
you can try this
[imageNav sizeToFit];
UIImage *image = [UIImage imageNamed:[imageTop objectAtIndex:0]]
UIImageView *imageNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
image.size.width, image.size.height)];
imageNav.image = image;
Hope above code snippet helps..

crush the height and width of the initWithFrame image size

I have a small problem with a UIImageView, I try to place an image dynamically so I start like this
self.picture_view = UIImageView.alloc.initWithFrame (CGRectMake (0, 16,45, 46))
but I want to take my frame size of each image I passed him.
So I do like this.
picture_frame = picture_view.frame;
picture_frame.size = picture_view.size;
picture_view.frame = picture_frame;
but when I do this
NSLog (picture_frame.size.inspect)
it gives me 45, 46 for each image,
So how recovered the image size and frame overrider for me that shows the correct size
thank you in advance.
PS: I done well picture_view.image = UIImage.imageNamed (my_picture)
You are actually setting the ImageViews frame to the frame of itself. You need to be doing it based on the actual UIImage. You can do this automatically by initing the view with the image.
Ex.
UIImageView *pictureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"whatever.png"]];
//The size is already set correctly, but if you want to change the x or y origin do like so
pictureView.frame = CGRectMake(0, 16, pictureView.frame.size.width, pictureView.frame.size.height);
Edit: This answer is written in Objective-C, but the core idea should translate to rubymotion very easily.
If you are trying to create a UIImageView and then resize it to fit the UIImage you assign to it, you should try something like this:
Create an imageView with an image:
UIImage *image = [UIImage imageNamed:#"myPicture.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
Change the image, and resize the imageView to fit:
UIImage *anotherImage = [UIImage imageNamed:#"anotherPicture.png"];
imageView.image = anotherImage;
CGRect imageViewFrame = imageView.frame;
imageViewFrame.size = anotherImage.size;
imageView.frame = imageViewFrame;

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.

UIColor colorWithPatternImage changes the color of the image

I have the following code.
//Create a view to place repeated arrow in.
UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
//Add Repeated Arrows to the top of the view using a color pattern.
UIImage *arrowImage = [UIImage imageNamed:#"arrow-down"];
UIColor *repeatedArrowColor = [UIColor colorWithPatternImage:arrowImage];
CGRect repeatedArrowFrame = CGRectMake(0, 0, 320.0, [arrowImage size].height);
UIView *repeatedArrowView = [[UIView alloc] initWithFrame:repeatedArrowFrame];
[repeatedArrowView setBackgroundColor:repeatedArrowColor];
[rootView addSubview:repeatedArrowView];
The image being used is a png with arrows colored #323232. I verified this by right clicking the .app file and selecting Show Package Contents.
However when the pattern appears on the iOS simulator screen the color of the arrows are #262626 or #252525.
Has anyone run into this problem? If so, how did you fix, or did you work-around it?
In short everyone has these problems. If you open that image in Preview and use pixie (dev tool), you will see a different value for the pixels than is in the file for sure. Is that value different from what you see in the Simulator?
Do your png's have a color profile embedded in them? It may help if there is one if you delete it (pngcrush will do this as an option). If there is none, or that does not fix it, then you may need to load your image first as a CGImageRef using:
CGImageRef CGImageCreateWithPNGDataProvider (
CGDataProviderRef source,
const CGFloat decode[],
bool shouldInterpolate,
CGColorRenderingIntent intent
);
and fool around with the color intent (first) and as a last option the decode array.

Resources