Display Image to iPhone Simulator Screen
I am just getting started with iPhone development. I've done some tutorials to get th开发者_如何学Ce general feel of Xcode and Objective C. I've been searching for the proper way to do this simple task but everything I have tried comes up empty. For now I am just trying to read in a .jpg file from my desktop and display it in the iPhone simulator. The code I am currently using is:
- ( void )viewDidLoad {
UIImage *img = [UIImage imageNamed:@"/Users/jinser/Desktop/cow.jpg"];
//UIImage *img = [UIImage imageNamed:@"cow.jpg"];
[imgView setImage:img];
[super viewDidLoad]; }
and in my Interface file I have:
@interface MainViewController: UIViewController {
IBOutlet UIImageView *imgView;
}
@end
I do not receive any errors, but the simulator opens up with no image displayed. I've also tried adding the image to my project resources and using the second line of code that I have commented about but it gives me the same result.
Any ideas? Ultimately I will use OpenCV functions to do some processing on the image, but i need the image to be read in correctly first obviously.
Thanks for any help!
It's quite complicated to answer your question without details. I made a simple project that reach you needs. Check it and you'll find out why your code doesn't work.
Sample Project
You need to add the file to your Xcode project. You cannot simply reference it as you've done.
You need to connect the image view to the file's owner in interface builder because you are using IBOutlet.
You schould use .png. I had the same problem by using other images like .jpg. I am not sure, but probably only .png images are allowed. Also, with .png you will not have any colour problems.
精彩评论