// 2, stretchableImageWithLeftCapWidth拉伸不变形
UIImage*image=[[UIImageimageNamed:@"leader1"]stretchableImageWithLeftCapWidth:10topCapHeight:10];
//1, 初始化一个imageview
UIImageView*imageView=[[UIImageViewalloc]initWithImage:image];
//3,等比率缩放
[selfscaleImage:imageView.imagetoScale:1/8];
imageView.frame=CGRectMake(10,nameTextField.bottom,ScreenWidth-20,headVIew.height-nameTextField.bottom);
[headVIewaddSubview:imageView];
//1.等比率缩放
- (UIImage*)scaleImage:(UIImage*)image toScale:(float)scaleSize
{
UIGraphicsBeginImageContext(CGSizeMake(image.size.width* scaleSize, image.size.height* scaleSize));
[imagedrawInRect:CGRectMake(0,0, image.size.width* scaleSize, image.size.height* scaleSize)];
UIImage*scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnscaledImage;
}
#pragma mark ----图片剪切到指定大小
- (UIImage*)cutImage:(UIImage*)image viewsize:(CGSize)viewsize
{
//压缩图片
CGSizenewSize;
CGImageRefimageRef =nil;
if((image.size.width/ image.size.height) < (viewsize.width/ viewsize.height)) {
newSize.width= image.size.width;
newSize.height= image.size.width* viewsize.height/ viewsize.width;
imageRef =CGImageCreateWithImageInRect([imageCGImage],CGRectMake(0,fabs(image.size.height- newSize.height) /2, newSize.width, newSize.height));
}else{
newSize.height= image.size.height;
newSize.width= image.size.height* viewsize.width/ viewsize.height;
imageRef =CGImageCreateWithImageInRect([imageCGImage],CGRectMake(fabs(image.size.width- newSize.width) /2,0, newSize.width, newSize.height));
}
return[UIImageimageWithCGImage:imageRef];
}
#pragma mark ----图片压缩到指定大小
- (UIImage*)scaleToSize:(UIImage*)img size:(CGSize)size
{
//创建一个bitmap的context,并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
//绘制改变大小的图片
[imgdrawInRect:CGRectMake(0,0, size.width, size.height)];
//从当前context中创建一个改变大小后的图片
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
//使当前的context出堆栈
UIGraphicsEndImageContext();
//返回新的改变大小后的图片
returnscaledImage;
}