使用UIImageView展現來自網絡的圖片


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 allocinitWithFrame:CGRectMake(00,32044)];
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!");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM