您的位置:首页 > 理论基础 > 计算机网络

使用UIImageView展现来自网络的图片

2015-12-25 22:53 543 查看



UIImageView:可以通过UIImage加载图片赋给UIImageView,加载后你可以指定显示的位置和大小。
1、初始化
UIImageView*imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0,45.0,300,300)];

imageView.image=[UIImageimageNamed:@"a.png"];//加载入图片

[self.viewaddSubView:image];

[imageViewrelease];

//imageNamed方法是不能通过路径进行加载图片的,此方式容易引起发生内存警告从而导致自动退出的问题。

//最好是通过直接读取文件路径[UIImageimageWithContentsOfFile]解决掉这个问题.
NSImage*image=[[NSImagealloc]initWithContentsOfURL:(NSURL*)];

NSImage*image=[[NSImagealloc]initWithContentsOfFile:(NSString*)];

如:
1、》》》
UIImage*image=[[UIImagealloc]initWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];

UIImageView*imageView=[[UIImageViewalloc]initWithImage:image];


2、》》》

NSString*path=[[NSBundlemainBundle]pathForResource:@”icon”ofType:@”png”];

NSImage*myImage=[UIImageimageWithContentsOfFile:path];


//让一个UIImageView响应点击事件

UIImageView*imgView=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,44)];

imgView.userInteractionEnabled=YES;

UITapGestureRecognizer*singleTap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(onClickImage)];

[imgViewaddGestureRecognizer:singleTap];

[singleTaprelease];

-(void)onClickImage{

//here,dowhateveryouwanttodo

NSLog(@"imageviewisclicked!");

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: