ios显示图片的方法

1、显示app内置的图片的方法

UIImage *myImage = [UIImage imageNamed:@"图片名"];  

然后用[self.uiimageview setImage:myImage]

2、显示手机内部的地址的图片

UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]]; 

3、在线加载图片

UIImage *myImage2 =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];  

4、下载图片和加载的方法

//下载图片
-(UIImage *) getImageFromURL:(NSString *)fileURL {
    NSLog(@"执行图片下载函数");
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    //下面是一句话搞定,上面是分开来做得。
    //UIImage * result = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]];
    return result;
}

//下面是定义ImageView的方法
-(void) initImageView{
    NSString * urlWeb=@"http://simg.sinajs.cn/blog7newtpl/image/30/30_1/images/sinablogb.jpg";
   
    UIImageView * imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , 200, 400)];
    imageView.backgroundColor=[UIColor blueColor];
   
    //两种方法,用imageNamed可以把图片放入内存,重复使用。但是太多会挂掉,一般重复使用的图片用imageNamed
    [imageView setImage:[self getImageFromURL:urlWeb]];
   
    //[imageView setImage:[UIImage imageNamed:@"desc_bn.png"]];
    [self.view addSubview:imageView];
}

最后在viewDidLoad函数里面调用initImageView方法就行了。

5、图片的属性控制

 //    (2)设置圆角

    imageView.layer.masksToBounds = YES;

    imageView.layer.cornerRadius = 10;

    

    

    //    (3)设置边框颜色和大小

    imageView.layer.borderColor = [UIColor orangeColor].CGColor;

    imageView.layer.borderWidth = 2;

    

    

    //    (4)contentMode属性:当图片小于imageView的大小处理图片显示



    //    这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:

    //

    //    UIViewContentModeScaleToFill :填充整个UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    

    

    //(5)播放一系列图片

    UIImage *image1 = [UIImage imageNamed:@"homeNaviLeftBtn"];

    UIImage *image2 = [UIImage imageNamed:@"homeNaviRightBtn"];

    UIImage *image3 = [UIImage imageNamed:@"image_photo"];

    NSArray *imagesArray = @[image1,image2,image3];

    imageView.animationImages = imagesArray;

    // 设定所有的图片在多少秒内播放完毕

    imageView.animationDuration = [imagesArray count];

    // 不重复播放多少遍,0表示无数遍

    imageView.animationRepeatCount = 0;

    // 开始播放

    [imageView startAnimating];

    

    

    

     //(6)为图片添加单击事件:一定要先将userInteractionEnabled置为YES,这样才能响应单击事件

    

    imageView.userInteractionEnabled = YES;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImageView:)];

    [imageView addGestureRecognizer:singleTap];

    

    

    

    //(7)其他设置

    

//    imageView.hidden = YES或者NO;    // 隐藏或者显示图片

    imageView.alpha =0.5;    // 设置透明度

    // 设置高亮时显示的图片

    //imageView.highlightedImage = (UIImage *)hightlightedImage;

    

     //imageView.image = (UIImage *)image; // 设置正常显示的图片


   

    

    //设置位置 1)修改center ImageView的中间点2)修改frame

    imageView.frame = CGRectMake(1066300400);

//    imageView.center = CGPointMake(0, 0);




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值