UIImageView和UIImage
2016-05-12 23:21
281 查看
1.UIImageView中的视图内容显示模式
UIImageView *imageV=[[UIImageViewalloc]init];
imageV.scaleToFill------默认缩放填充;
imageV.scaleAspectfit-------有留白;
imageV.AspectFill------自适应填充,视图会被裁剪;
2.UIImageView显示圆形图片
设置圆形图像的原理很简单,通过设置UIImageView的圆角属性即可。首先我们需要保证待设置的图片资源大小为方形的(稍后我们会提供图像裁剪方法)。方法一
+++++++++++
方法一
UIImageView *imageView1 = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"11.png"]];
imageView1.frame = CGRectMake(60,100, 100, 100);
imageView1.layer.masksToBounds =YES;
imageView1.layer.cornerRadius =50;
[self.view addSubview:imageView1];
++++++++++++++++++++++++
方法二:
虽然我们这个demon中并没有用到图片的裁剪,但是很有可能实践项目中会涉及到图片裁剪成正方形。裁剪算法也很简单,以最短边边长为裁剪正方形的边长,在图像剧中的位置进行裁剪。
=+++++++++++++++++++++++++++++++++++++++++++++++
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 前
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 = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"11.png"]];
imageView1.frame = CGRectMake(60,100, 100, 100);
imageView1.layer.masksToBounds =YES;
imageView1.layer.cornerRadius =50;
[self.view addSubview:imageView1];
++++++++++++++++++++++++
方法二:
UIImageView *imageView2 = [[UIImageViewalloc] initWithFrame:CGRectMake(60,250, 100,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 前
相关文章推荐
- HDU3397 Sequence operation(线段树的区间合并)
- SDWebImage与UIScrollView或UICollectionView一起使用多次收到“Received memory warning.”,最终程序闪退的原因分析和解决办法。
- forward(request,reponse)与sendRedirect
- 可修改UID的白卡,请问为什么一般读写器不能修改这种卡的UID,必须要特定的读写器才能修改?
- iOS开发封装带有Button的UIView控件,使用target-action模式给button添加点击事件
- 129.Longest Consecutive Sequence
- iOS开发封装带有Button的UIView控件,使用代理给button添加点击事件
- deep learning---利用caffe在vgg-face上finetuing自己的人脸数据
- mui图片预览
- weblogic 下异常 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken
- 62. Unique Paths
- leetcode_347 Top K Frequent Elements
- Guid的使用
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- String StringBuffer与StringBuilder的区别
- Java 设计模式-建造者模式(Builder)Android讲解
- POJ 3061 - Subsequence
- StringBuilder的使用
- ugui 添加事件的几种方式
- Guid的使用