//molakeFramework
//Created by molake on 16/1/13.
//Copyright © 2016年molake. All rights reserved.
#import"UIImage+UIImageExtras.h"
@implementationUIImage (UIImageExtras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize
{
UIImage *sourceImage =self;
UIImage *newImage =nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor =0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize, targetSize) ==NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if(widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;
}elseif(widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width= scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage ==nil)
NSLog(@"could not scale image");
returnnewImage ;
}
+ (UIImage *)clipFromView: (UIView *) theView
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returntheImage;
}
+(UIImage *)clipFromView:(UIView *)theView andFrame:(CGRect)rect
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIRectClip(rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
//[theView.layer renderInContext:context];
UIGraphicsEndImageContext();
returntheImage;
}
+ (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
returnnewImage;
}
@end
UIImage裁剪
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 需求是需要上面圆弧效果️PS:用贝塞尔曲线实现圆弧效果涉及到轻微的高中几何知识...可以自行百度,其实只需要求出半...
- 最近对图片的处理较多,就总结出一个UIImage分类(UIImage+ZJIamge.h),以便之后使用。 此分类...
- 首先说一点:国足赢了,苯宝宝表示很高兴。 本文是继上篇文章之[UIImage系列]-图片的任意旋转、剪切及拉伸之后...