简体   繁体   中英

Get UIImage to show in UIImageView

My view does not show.

I created an empty application and added a button and an UIImageView.

I used storyboards. There is only one storyboard. An arrow points to this storyboard.

I set the rootViewController in the AppDelegate. I set the image below in the view controller.

The image is listed as 'bl_5_00' in the Images.xcassets blue folder.

A red button doesn't show either which leads me to believe I have messed something else up that is more structural.

App Delegate

#import "PEPI_AppDelegate.h"
#import "PEPI_LessonViewController.h"

@implementation PEPI_AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    PEPI_LessonViewController *controller = [[PEPI_LessonViewController alloc] init];
    self.window.rootViewController = controller;

    [self.window makeKeyAndVisible];
    return YES;
}

View Controller

#import "PEPI_LessonViewController.h"

@interface PEPI_LessonViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *mainImage;

@end

@implementation PEPI_LessonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImage *imageYouWantToPass = [UIImage imageNamed:@"bl_5_00"];

    self.mainImage.image = imageYouWantToPass;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Image with content of file doesn't work if you pass image name. You can use imageNamed instead:

UIImage *imageYouWantToPass = [UIImage imageNamed:@"bl_5_00"];

I asume l_5_00 is name of the image and you added this file to your project (not just reference).

If you use

[UIImage imageWithContentsOfFile:@"bl_5_00"];

that means you are trying to fetch the image from an absolute file path, and here you are passing relative name which will not work. You can try instead..

NSString *filePath=[[NSBundle mailBundle] pathForResource:@"bl_5_00" ofType:@"png"];

OR

NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"bl_5_00.png"];

and then

UIImage *image=[UIImage imageWithContentsOfFile:filePath];

Edited

To check whether the file is in your asset or, not try the following trick just add

NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"bl_5_00" ofType:@"png"]);

It will log the full path of the image if exist, I suspect it is not added in your bundle so its not loading, you will get (null) if image doesn't exist. Try to re-add the image and clean build the app again.

Hope it helps. Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM