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?
This question is related to
objective-c
ios
cocoa-touch
You can set an Image in UIImage object eiter of the following way:
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:
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];
UIImage *img = [UIImage imageNamed@"aImageName"];
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];
There is an error on this line:
[img setImage:[UIImage imageNamed@"anyImageName"]];
It should be:
[img setImage:[UIImage imageNamed:@"anyImageName"]];
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];
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);
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.
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.
just change this line
[img setImage:[UIImage imageNamed:@"anyImageName"]];
with following line
img = [UIImage imageNamed:@"anyImageName"];
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];
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];
Source: Stackoverflow.com