UIImageView和UIImage

来源:互联网 发布:2016淘宝不能邮箱注册 编辑:程序博客网 时间:2024/05/14 18:42

1.UIImageView中的视图内容显示模式

    UIImageView *imageV=[[UIImageViewalloc]init];

    imageV.scaleToFill------默认缩放填充;

    imageV.scaleAspectfit-------有留白;

    imageV.AspectFill------自适应填充,视图会被裁剪;



2.UIImageView显示圆形图片

圆形图像

设置圆形图像的原理很简单,通过设置UIImageView的圆角属性即可。首先我们需要保证待设置的图片资源大小为方形的(稍后我们会提供图像裁剪方法)。方法一
//设置图像显示控件为圆形    - (void)changeToCirclePicture    {        //设置圆角半径为方形边长一半        [self.imageView.layer setCornerRadius:CGRectGetHeight([self.imageView bounds]) / 2];        [self.imageView.layer setMasksToBounds:YES];        //设置边框宽度和颜色        [self.imageView.layer setBorderWidth:10];        [self.imageView.layer setBorderColor:[[UIColor grayColor] CGColor]];    }
+++++++++++

方法一

   UIImageView *imageView1 = [[UIImageViewallocinitWithImage:[UIImageimageNamed:@"11.png"]];

    imageView1.frame = CGRectMake(60,100100100);

    imageView1.layer.masksToBounds =YES;

    imageView1.layer.cornerRadius =50;

    [self.view addSubview:imageView1];

++++++++++++++++++++++++
方法二:

    UIImageView *imageView2 = [[UIImageViewallocinitWithFrame:CGRectMake(60,250100,100)];

    UIImage *image2 = [UIImageimageNamed:@"12.png"];

    imageView2.image = [self circleImage:image2 withParam:0];//调用下面的方法

    [self.view addSubview:imageView2];



-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {

    UIGraphicsBeginImageContext(image.size);

    CGContextRef context =UIGraphicsGetCurrentContext();

    //圆的边框宽度为2,颜色为红色

    CGContextSetLineWidth(context,2);

    CGContextSetStrokeColorWithColor(context, [UIColorredColor].CGColor);

    CGRect rect = CGRectMake(inset, inset, image.size.width - inset *2.0f, image.size.height - inset *2.0f);

    CGContextAddEllipseInRect(context, rect);

    CGContextClip(context);

    //在圆区域内画出image原图

    [image drawInRect:rect];

    CGContextAddEllipseInRect(context, rect);

    CGContextStrokePath(context);

    //生成新的image

    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newimg;

}





3.图像裁剪成正方形显示

虽然我们这个demon中并没有用到图片的裁剪,但是很有可能实践项目中会涉及到图片裁剪成正方形。裁剪算法也很简单,以最短边边长为裁剪正方形的边长,在图像剧中的位置进行裁剪。

//截取居中的方形图像    - (UIImage *)cutPicture:(UIImage *)raw    {        CGSize origImageSize = raw.size;        CGRect newRect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH);        float ratio = MAX(newRect.size.width / origImageSize.width, newRect.size.height / origImageSize.height);        //开启透明位图上下文        UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);        //创建圆角矩形的对象,这里设置圆角为0        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:0.0];        //裁剪图形上下文        [path addClip];        //让图片在缩略图绘制范围内居中        CGRect projectRect;        projectRect.size.width = ratio * origImageSize.width;        projectRect.size.height = ratio * origImageSize.height;        projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;        projectRect.origin.y = (newRect.size.height - projectRect.size.height) / 2.0;        //在上下文中绘制图片        [raw drawInRect:projectRect];        //从上下文获取图片,并复制给item        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();        //清理图形上下文        UIGraphicsEndImageContext();        return smallImage;    }






=+++++++++++++++++++++++++++++++++++++++++++++++
UIIMage
  1.创建UIIMage
 

    UIImage *img=[UIIMage imageWithCGImage:<#(nonnull CGImageRef)#>];//加载的时候,1个像素就是一个点;

    UIImage *img=[UIImage imageWithCGImage:<#(nonnull CGImageRef)#> scale:<#(CGFloat)#> orientation:<#(UIImageOrientation)#>];//可以设置缩放比例


    UIImage *img=[UIImage imageNamed:<#(nonnull NSString *)#>];//图片会常驻内存中,直接从内存读取

    UIImage *img=[UIImage imageWithContentsOfFile:<#(nonnull NSString *)#>];//不做缓存,bundle路径中加载.速度慢


    UIImage *image=[btn bacgaroundImageForState:UIViewControllerShowDetailTargetDidChangeNotification];//btn是按钮,从按钮中获取图片


    //将图片转换成二进制

   NSData *data= UIImagePNGRepresentation(img);



2.图片拉伸

    UIImage *resizeImage=[img resizableImageWithCapsets:UIEdgeInsetsMake(self.view.bounds.size.width/2,self.view.bounds.size.width/2,self.view.bounds.size.width/2,self.view.bounds.size.width/2) resizingmode:UIImageResizingModeStretch];//UIImageResizingModeTitle是平铺,iOS5.0后


UIImage  *image=[img stretchableImageWithleftcapWidth:self.view.bounds.size.width*0.5  topcapheight:self.view.bounds.size.height*0.5]; //iOS5.0 前



0 0