美文网首页
一些收集的工具类-image

一些收集的工具类-image

作者: 哆啦_ | 来源:发表于2016-06-17 13:51 被阅读70次

    下载链接:http://download.csdn.net/my

    1

    /**

    *  根据颜色绘制一张图片 可以用来设置button不同状态下的背景颜色

    *

    *  @param color color

    *  @param size  size

    *

    *  @return 返回绘制的image

    */

    + (UIImage *)imageWithColor:(UIColor *)color  size:(CGSize)size

    {

    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

    }

    /**

    *  裁剪image

    *

    *  @param rect  裁剪的rect

    *  @param image 要裁剪的image

    *

    *  @return 返回裁剪之后的image

    */

    + (UIImage*)subImageInRect:(CGRect)rect withImage:(UIImage *)image

    {

    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

    UIGraphicsBeginImageContext(smallBounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextDrawImage(context, smallBounds, subImageRef);

    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

    UIGraphicsEndImageContext();

    CFRelease(subImageRef);

    return smallImage;

    }

    // 缩放从顶部开始平铺图片

    + (UIImage *)imageScaleAspectFillFromTop:(CGSize)frameSize withImage:(UIImage *)image

    {

    CGFloat screenScale = [UIScreen mainScreen].scale;

    CGFloat radio = image.size.height / image.size.width;

    CGFloat height = frameSize.height / radio;

    UIImage *adjustedImg = [self scaleToSize:CGSizeMake(frameSize.width * screenScale, height) withImage:image];

    // 裁剪

    CGRect rect = CGRectMake(0, 0, frameSize.width * screenScale,frameSize.width * screenScale);

    adjustedImg = [self subImageInRect:rect withImage:adjustedImg];

    return adjustedImg;

    }

    /**

    *  等比例缩放图片

    *

    *  @param size  size

    *  @param image 要缩放的image

    *

    *  @return 返回缩放的image

    */

    + (UIImage *)scaleToSize:(CGSize)size withImage:(UIImage *)image

    {

    CGFloat width = CGImageGetWidth(image.CGImage);

    CGFloat height = CGImageGetHeight(image.CGImage);

    float verticalRadio = size.height*1.0/height;

    float horizontalRadio = size.width*1.0/width;

    float radio = 1;

    if(verticalRadio>1 && horizontalRadio>1)

    {

    radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;

    }

    else

    {

    radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;

    }

    width = width*radio;

    height = height*radio;

    int xPos = (size.width - width)/2;

    int yPos = (size.height-height)/2;

    // 创建一个bitmap的context

    // 并把它设置成为当前正在使用的context

    UIGraphicsBeginImageContext(size);

    // 绘制改变大小的图片

    [image drawInRect:CGRectMake(xPos, yPos, width, height)];

    // 从当前context中创建一个改变大小后的图片

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    // 使当前的context出堆栈

    UIGraphicsEndImageContext();

    // 返回新的改变大小后的图片

    return scaledImage;

    }

    + (UIImage *)imageFillSize:(CGSize)viewsize withImage:(UIImage *)image

    {

    CGSize size = image.size;

    CGFloat scalex = viewsize.width / size.width;

    CGFloat scaley = viewsize.height / size.height;

    CGFloat scale = MAX(scalex, scaley);

    UIGraphicsBeginImageContext(viewsize);

    CGFloat width = size.width * scale;

    CGFloat height = size.height * scale;

    float dwidth = ((viewsize.width - width) / 2.0f);

    float dheight = ((viewsize.height - height) / 2.0f);

    CGRect rect = CGRectMake(dwidth, dheight, size.width * scale, size.height * scale);

    [image drawInRect:rect];

    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newimg;

    }

    /**按照指定宽度按比例缩放图片**/

    + (UIImage *)zoomImage:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth

    {

    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    CGFloat targetWidth = defineWidth;

    CGFloat targetHeight = height / (width / targetWidth);

    CGSize size = CGSizeMake(targetWidth, targetHeight);

    CGFloat scaleFactor = 0.0;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

    if(CGSizeEqualToSize(imageSize, size) == NO){

    CGFloat widthFactor = targetWidth / width;

    CGFloat heightFactor = targetHeight / height;

    if(widthFactor > heightFactor){

    scaleFactor = widthFactor;

    }

    else{

    scaleFactor = heightFactor;

    }

    scaledWidth = width * scaleFactor;

    scaledHeight = height * scaleFactor;

    if(widthFactor > heightFactor){

    thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

    }else if(widthFactor < heightFactor){

    thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

    }

    }

    UIGraphicsBeginImageContext(size);

    CGRect thumbnailRect = CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil){

    NSLog(@"scale image fail");

    }

    UIGraphicsEndImageContext();

    return newImage;

    }

    相关文章

      网友评论

          本文标题:一些收集的工具类-image

          本文链接:https://www.haomeiwen.com/subject/ctdvdttx.html