UIImageView:可以通過UIImage加載圖片賦給UIImageView,加載后你可以指定顯示的位置和大小。
1、初始化
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0,45.0,300,300)];
imageView.image = [UIImage imageNamed:@"a.png"];//加載入圖片
[self.view addSubView:image];
[imageView release];
//imageNamed方法是不能通過路徑進行加載圖片的,此方式容易引起發生內存警告從而導致自動退出的問題。
//最好是通過直接讀取文件路徑[UIImage imageWithContentsOfFile]解決掉這個問題.
NSImage *image = [[NSImage alloc]initWithContentsOfURL:(NSURL *)];
NSImage *image = [[NSImage alloc]initWithContentsOfFile:(NSString *)];
如:
1、》》》
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
2、》》》
NSString *path = [[NSBundle mainBundle]pathForResource:@”icon”ofType:@”png”];
NSImage *myImage = [UIImage imageWithContentsOfFile:path];
//讓一個UIImageView響應點擊事件
UIImageView *imgView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320, 44)];
imgView.userInteractionEnabled=YES;
UITapGestureRecognizer *singleTap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(onClickImage)];
[imgView addGestureRecognizer:singleTap];
[singleTap release];
-(void)onClickImage{
// here, do whatever you wantto do
NSLog(@"imageview is clicked!");
}