22

How do I display an image in the UIImageView component from the hard drive?

I have a local folder called "images" and would like to access it via a relative path, since I want to package the images with the app.

Cœur
  • 37,241
  • 25
  • 195
  • 267
mirzahat
  • 1,025
  • 2
  • 10
  • 19

4 Answers4

59

To load image from somewhere on your drive use:

UIImage * myImage = [UIImage imageWithContentsOfFile: @"../images/1.png"];

Or add image to the Xcode project and use:

UIImage * myImage = [UIImage imageNamed: @"1.png"];

After that add image to imageview:

UIImageView * myImageView = [[UIImageView alloc] initWithImage: myImage];
Valerii Pavlov
  • 1,986
  • 1
  • 19
  • 30
  • 2
    Since you want to package it with your app you should use the last two lines of code here, not the top one. You should also drag that image from your hard drive to a folder in your project, like the Resources folder. – tazboy Jul 13 '11 at 06:39
9

You dont need to give any path as long as the image is in your resources folder.

You can display that image in the imageView using this.

yourImageView.image = [UIImage imageNamed:@"something.png"];
Robin
  • 10,011
  • 5
  • 49
  • 75
2
UIImage *img = [UIImage imageWithContentsOfFile:(the file path)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];

check the docs for imageWithContentsOfFile:

Mridul Kashatria
  • 4,157
  • 2
  • 18
  • 15
0

What do you mean by images in hard drive.Are you saying that you have kept your images in your bundle in iphone app.If so then you can make array of those images like this....

NSMutableArray *imageArray;
imageArray=[[NSMutableArray alloc]initWithObjects:@"001.jpg",@"002.jpg",@"003.jpg",@"004.jpg",nil];

And then can access your images by this array;

Or if you simply want to show a single image from the bundle you can do this way

UIImageView *creditCardImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 16, 302, 77)];
    creditCardImageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"CreditCard_2RowTable" ofType:@"png"]];
    [self.view addSubview:creditCardImageView];

This will help you.

Sabby
  • 2,586
  • 2
  • 27
  • 41