33

How can I set image to UIImage object. For example:

UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@"anyImageName"]];

My UIImage object is declared in .h file and allocated in init method, I need to set image in viewDidLoad method. Any ideas?

Shamsiddin Saidov
  • 2,281
  • 4
  • 23
  • 35

12 Answers12

58
UIImage *img = [UIImage imageNamed:@"anyImageName"];

imageNamed:
Returns the image object associated with the specified filename.

    + (UIImage *)imageNamed:(NSString *)name

Parameters
name
The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.
Return Value
The image object for the specified file, or nil if the method could not find the specified image.

Discussion
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
11

Create a UIImageView and add UIImage to it:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image Name"]] ;

Then add it to your view:

[self.view addSubView: imageView];
Ankit Jain
  • 1,858
  • 22
  • 35
Shailesh
  • 3,072
  • 3
  • 24
  • 33
2

Just Follow This

UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 300, 400)];
[imgview setImage:[UIImage imageNamed:@"YourImageName"]];
[imgview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imgview];
Dixit Akabari
  • 2,419
  • 13
  • 26
1

There is an error on this line:

[img setImage:[UIImage imageNamed@"anyImageName"]]; 

It should be:

[img setImage:[UIImage imageNamed:@"anyImageName"]]; 
WhoaItsAFactorial
  • 3,538
  • 4
  • 28
  • 45
1
UIImage *img = [UIImage imageNamed@"aImageName"];
Maulik
  • 19,348
  • 14
  • 82
  • 137
1

may be:

UIImage *img = [[UIImage alloc] init];

and when you want to change the image:

img = [UIImage imageNamed:@"nameOfPng.png"];

but the object wasn't in the same place in the memory, but if you use the pointer, the same pointer will be point to the last image loaded.

Vincent Saluzzo
  • 1,377
  • 1
  • 11
  • 13
0

Like vikingosgundo said, but keep in mind that if you use [UIImage imageNamed:image] then the image is cached and eats away memory. So unless you plan on using the same image in many places, you should load the image, with imageWithContentsOfFile: and imageWithData:

This will save you significant memory and speeds up your app.

Luke
  • 11,426
  • 43
  • 60
  • 69
Trausti Thor
  • 3,722
  • 31
  • 41
0

First declare UIImageView and give it frame

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake( 10.0f, 15.0f, 40.0f,40.0f )];
        [imageView setBackgroundColor: [UIColor clearColor]];
        [imageView setImage:[UIImage imageNamed:@"comments_profile_image.png"]];
        [self.view addSubview: imageView];
Ankit Jain
  • 1,858
  • 22
  • 35
0

You can set an Image in UIImage object eiter of the following way:

  1. UIImage *imageObject = [UIImage imageNamed:@"imageFileName"]; but by initializing UIImage Object like this, the image is always stored in cache memory even you make it nil like imageObject=nil;

Its better to follow this:

  1. NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"imageFilename" ofType:@"imageFile_Extension"];

    UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];
    

EXAMPLE: NSString *imageFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"png"];

UIImage *imageObject = [UIImage imageWithContentsOfFile:imageFilePath];
Ishank
  • 2,860
  • 32
  • 43
Satish Azad
  • 2,302
  • 1
  • 16
  • 35
0

just change this line

[img setImage:[UIImage imageNamed:@"anyImageName"]];

with following line

img = [UIImage imageNamed:@"anyImageName"];
BHASKAR
  • 1,201
  • 1
  • 9
  • 20
0

You can't alloc UIImage, this is impossible. Proper code with UIImageView allocate:

UIImageView *_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"something.png"]] ;

Draw image:

CGContextDrawImage(context, rect, _image.image.CGImage);
theWalker
  • 2,022
  • 2
  • 18
  • 27
0

Try this code to 100% work....

UIImageView * imageview = [[UIImageView alloc] initWithFrame:CGRectMake(20,100, 80, 80)];
imageview.image = [UIImage imageNamed:@"myimage.jpg"];
[self.view  addSubview:imageview];
P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21