PathForResource Method

enter image description hereI play with imageView and there is a method [imageWithContentsOfFile]; But actually it shows nothing after launch. Below the code there is a method [imageNamed:], and it works as usual. Any ideas? Thank,

#import "ViewController2.h"

@interface ViewController2 ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController2


- (BOOL)prefersStatusBarHidden
{
    return YES;
}


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


    // just fugly don't do it

//    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stanford"]];


    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];

    NSArray *actionButtonItems = @[shareItem, cameraItem];
    self.navigationItem.rightBarButtonItems = actionButtonItems;




    NSString *imgPath =[[NSBundle mainBundle] pathForResource:@"google" ofType:@"png"];
    self.imageView.image = [UIImage imageWithContentsOfFile:imgPath];


   // self.imageView.image = [UIImage imageNamed:@"google"];


}




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




@end

I think the problem is that the pathForResource method does not work correctly ...

+3
source share
3 answers

If you are targeting iOS 7+ and using asset directories (which it looks like you are using since you specified images.xcassets in one of your comments), Xcode 5 now transfers assets to the new file format, 1 for all assets. This means that you cannot access the file directly using imageWithContentsOfFile:. And this is also why the pathForResource:ofType:path does not return.

, .

, . imageNamed:. .

+4

breakPoint "imgPath". , , @2x, :

NSString * imgPath = [[NSBundle mainBundle] pathForResource: @ "google @2x" ofType: @ "png" ];

0

- > Navigator

- >

- >

- >

- > .

- > . .

0

All Articles