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 = [[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 *image=[UIImage imageWithData:data];
UIImage *image2=[UIImage imageWithData:data scale:0.5];
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 前
++++++++++++绘图封装一个背景图片
//首先封装了一个方法,用来生成背景图片
- (UIImage *) imageWithFrame:(CGRect)frame alphe:(CGFloat)alphe {
frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
UIColor *redColor = [UIColorcolorWithRed:0green:0blue:0alpha:alphe];
UIGraphicsBeginImageContext(frame.size);
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [redColorCGColor]);
CGContextFillRect(context, frame);
UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
===========
直接用图片设置View的背景,会因版本和手机大小产生变形,解决办法,创建一个UIIMageview 加到View上;
========计算图片占内存的大小;
NSData * imageData = UIImageJPEGRepresentation(image,1);
length = [imageData length]/1000;//获得是MB
==============
SDWEIMage回调中获取宽高;
UIImageView *imageView = [[UIImageView alloc]init];
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://"] placeholderImage:niloptions:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"宽:%f, 高:%f", image.size.width, image.size.height);
}];
第二种方法:一行代码获取图片尺寸:
先引入系统的ImageIO.framework库
CGSize size = [UIImage getImageSizeWithURL:@"http://"];
NSLog(@"宽:%f, 高:%f", size.width, size.height);
计算图片的size:
UIImage *image=[UIImage imageWithData:data];
图片的尺寸:image.size
===============图片的下载:
方法一:imageUrl是图片的下载地址
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
// UIImage *image=[UIImage imageWithData:data];
// [self.imageIMArr addObject:image];
方法二:
#import <SDWebImageDownloader.h>
SDWebImageDownloader *downloader = [SDWebImageDownloadersharedDownloader];
[downloader downloadImageWithURL:[NSURLURLWithString:imageUrl]
options:0
progress:^(NSInteger receivedSize,NSInteger expectedSize) {
}
completed:^(UIImage *image,NSData *data,NSError *error,BOOL finished) {
if (image && finished) {
//缩小图片
CGSize newSize=CGSizeMake(118,118);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
image= [UIGraphicsGetImageFromCurrentImageContext()imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//从图形上下文获取新的图片
UIGraphicsEndImageContext();
[self.imageIMArraddObject:image];
if(self.imageIMArr.count==self.dataArr.count){
[self.tabreloadData];
NSLog(@"sdwebxiazai ");
[weakSelf.tab.headerendRefreshing];
}
}
}];
===========UIIMageView分类:
#import <UIKit/UIKit.h>
@interface UIImageView (LYImageview)
+(UIImageView *)initImageviewWithDict:(NSDictionary *)dict;
@end
#import "UIImageView+LYImageview.h"
@implementation UIImageView (LYImageview)
+(UIImageView *)initImageviewWithDict:(NSDictionary *)dict{
UIImageView *imageV=[[UIImageViewalloc]init];
imageV.image=[UIImageimageNamed:dict[@"image"]];
imageV.contentMode=
UIViewContentModeScaleToFill;//图片显示模式
// UIViewContentModeScaleToFill, 使图片缩放来填充满imageView.
// UIViewContentModeScaleAspectFit, 使图片保持原比例, 并且全部显示在imgeView上, 但是可能导致空白区域.
// UIViewContentModeScaleAspectFill, 使图片保持原比例, 并且填充满整个imageView, 但可能会导致只显示部分图片.
return imageV;
}
@end
调用:
NSDictionary *imageDict=@{
@"image":@"left"
};
UIImageView *imageV=[UIImageViewinitImageviewWithDict:imageDict];
imageV.frame=CGRectMake(50,150, 100, 100);
[self.viewaddSubview:imageV];
============================UIImage imagewithcontentfile:
+ (UIImage *)imageWithFileName:(NSString *)name {
NSString *extension = @"png";
NSArray *components = [name componentsSeparatedByString:@"."];
if ([components count] >= 2) {
NSUInteger lastIndex = components.count - 1;
extension = [components objectAtIndex:lastIndex];
name = [name substringToIndex:(name.length-(extension.length+1))];
}
// 如果为Retina屏幕且存在对应图片,则返回Retina图片,否则查找普通图片
if ([UIScreen mainScreen].scale == 2.0) {
name = [name stringByAppendingString:@"@2x"];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path != nil) {
return [UIImage imageWithContentsOfFile:path];
}
}
if ([UIScreen mainScreen].scale == 3.0) {
name = [name stringByAppendingString:@"@3x"];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path != nil) {
return [UIImage imageWithContentsOfFile:path];
}
}
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
if (path) {
return [UIImage imageWithContentsOfFile:path];
}
return nil;
}
+(UIImage *)imageNamedss:(NSString *)name{
//高效
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [resourcePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",name]];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
return image;
}