How can I set image to UIImage
object.
For example:
UIImage *img = [[UIImage alloc] init];
[img setImage:[UIImage imageNamed:@\"anyImageName\"]]
There is an error on this line:
[img setImage:[UIImage imageNamed@"anyImageName"]];
It should be:
[img setImage:[UIImage imageNamed:@"anyImageName"]];
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];
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.
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];
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);