Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImage initWithContentsOfFile doesn't work

I have question: I want to avoid [UIImage imageNamed:]. So i did:

UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:@"myimage.png"]; 
controller.productImg.image = prodImg;  
[prodImg release];

But now the image is not shown anymore.

Does anyone know how to get that to work?

like image 855
Tanner Avatar asked Sep 23 '11 06:09

Tanner


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do you declare UIImage in Objective C?

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.

What is UIImage in Xcode?

An object that manages image data in your app.


1 Answers

The above code is not working because correct way to do it is-

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"default" ofType:@"jpeg"];
UIImage *prodImg = [UIImage imageWithContentsOfFile:thePath]; 
controller.productImg.image = prodImg;  
[prodImg release];

;)

like image 126
Devarshi Avatar answered Oct 04 '22 20:10

Devarshi