UIImageView image not showing up

2019-08-16 20:59发布

问题:

_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.

回答1:

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 :)



回答2:

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