很有用的UIImage扩展方法(category)支持放大和旋转

  1. //
  2. //  UIImage-Extensions.h
  3. //
  4. //  Created by Hardy Macia on 7/1/09.
  5. //  Copyright 2009 Ca*****ount Software. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. @interface UIImage (CS_Extensions)
  9. - (UIImage *)imageAtRect:(CGRect)rect;
  10. - (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize;
  11. - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;
  12. - (UIImage *)imageByScalingToSize:(CGSize)targetSize;
  13. - (UIImage *)imageRotatedByRadians:(CGFloat)radians;
  14. - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees;
  15. @end;
复制代码
  1. //
  2. //  UIImage-Extensions.m
  3. //
  4. //  Created by Hardy Macia on 7/1/09.
  5. //  Copyright 2009 Ca*****ount Software. All rights reserved.
  6. //
  7. #import "UIImage-Extensions.h"
  8. CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
  9. CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};
  10. @implementation UIImage (CS_Extensions)
  11. -(UIImage *)imageAtRect:(CGRect)rect
  12. {
  13.   
  14.    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect);
  15.    UIImage* subImage = [UIImage imageWithCGImage: imageRef];
  16.    CGImageRelease(imageRef);
  17.   
  18.    return subImage;
  19.   
  20. }
  21. - (UIImage *)imageByScalingProportionallyToMinimumSize:(CGSize)targetSize {
  22.   
  23.    UIImage *sourceImage = self;
  24.    UIImage *newImage = nil;
  25.   
  26.    CGSize imageSize = sourceImage.size;
  27.    CGFloat width = imageSize.width;
  28.    CGFloat height = imageSize.height;
  29.   
  30.    CGFloat targetWidth = targetSize.width;
  31.    CGFloat targetHeight = targetSize.height;
  32.   
  33.    CGFloat scaleFactor = 0.0;
  34.    CGFloat scaledWidth = targetWidth;
  35.    CGFloat scaledHeight = targetHeight;
  36.   
  37.    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  38.   
  39.    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
  40.       
  41.       CGFloat widthFactor = targetWidth / width;
  42.       CGFloat heightFactor = targetHeight / height;
  43.       
  44.       if (widthFactor > heightFactor)
  45.          scaleFactor = widthFactor;
  46.       else
  47.          scaleFactor = heightFactor;
  48.       
  49.       scaledWidth  = width * scaleFactor;
  50.       scaledHeight = height * scaleFactor;
  51.       
  52.       // center the image
  53.       
  54.       if (widthFactor > heightFactor) {
  55.          thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  56.       } else if (widthFactor < heightFactor) {
  57.          thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  58.       }
  59.    }
  60.   
  61.   
  62.    // this is actually the interesting part:
  63.   
  64.    UIGraphicsBeginImageContext(targetSize);
  65.   
  66.    CGRect thumbnailRect = CGRectZero;
  67.    thumbnailRect.origin = thumbnailPoint;
  68.    thumbnailRect.size.width  = scaledWidth;
  69.    thumbnailRect.size.height = scaledHeight;
  70.   
  71.    [sourceImage drawInRect:thumbnailRect];
  72.   
  73.    newImage = UIGraphicsGetImageFromCurrentImageContext();
  74.    UIGraphicsEndImageContext();
  75.   
  76.    if(newImage == nil) NSLog(@"could not scale image");
  77.   
  78.   
  79.    return newImage ;
  80. }
  81. - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {
  82.   
  83.    UIImage *sourceImage = self;
  84.    UIImage *newImage = nil;
  85.   
  86.    CGSize imageSize = sourceImage.size;
  87.    CGFloat width = imageSize.width;
  88.    CGFloat height = imageSize.height;
  89.   
  90.    CGFloat targetWidth = targetSize.width;
  91.    CGFloat targetHeight = targetSize.height;
  92.   
  93.    CGFloat scaleFactor = 0.0;
  94.    CGFloat scaledWidth = targetWidth;
  95.    CGFloat scaledHeight = targetHeight;
  96.   
  97.    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  98.   
  99.    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
  100.       
  101.       CGFloat widthFactor = targetWidth / width;
  102.       CGFloat heightFactor = targetHeight / height;
  103.       
  104.       if (widthFactor < heightFactor)
  105.          scaleFactor = widthFactor;
  106.       else
  107.          scaleFactor = heightFactor;
  108.       
  109.       scaledWidth  = width * scaleFactor;
  110.       scaledHeight = height * scaleFactor;
  111.       
  112.       // center the image
  113.       
  114.       if (widthFactor < heightFactor) {
  115.          thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  116.       } else if (widthFactor > heightFactor) {
  117.          thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  118.       }
  119.    }
  120.   
  121.   
  122.    // this is actually the interesting part:
  123.   
  124.    UIGraphicsBeginImageContext(targetSize);
  125.   
  126.    CGRect thumbnailRect = CGRectZero;
  127.    thumbnailRect.origin = thumbnailPoint;
  128.    thumbnailRect.size.width  = scaledWidth;
  129.    thumbnailRect.size.height = scaledHeight;
  130.   
  131.    [sourceImage drawInRect:thumbnailRect];
  132.   
  133.    newImage = UIGraphicsGetImageFromCurrentImageContext();
  134.    UIGraphicsEndImageContext();
  135.   
  136.    if(newImage == nil) NSLog(@"could not scale image");
  137.   
  138.   
  139.    return newImage ;
  140. }
  141. - (UIImage *)imageByScalingToSize:(CGSize)targetSize {
  142.   
  143.    UIImage *sourceImage = self;
  144.    UIImage *newImage = nil;
  145.   
  146.    //   CGSize imageSize = sourceImage.size;
  147.    //   CGFloat width = imageSize.width;
  148.    //   CGFloat height = imageSize.height;
  149.   
  150.    CGFloat targetWidth = targetSize.width;
  151.    CGFloat targetHeight = targetSize.height;
  152.   
  153.    //   CGFloat scaleFactor = 0.0;
  154.    CGFloat scaledWidth = targetWidth;
  155.    CGFloat scaledHeight = targetHeight;
  156.   
  157.    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  158.   
  159.    // this is actually the interesting part:
  160.   
  161.    UIGraphicsBeginImageContext(targetSize);
  162.   
  163.    CGRect thumbnailRect = CGRectZero;
  164.    thumbnailRect.origin = thumbnailPoint;
  165.    thumbnailRect.size.width  = scaledWidth;
  166.    thumbnailRect.size.height = scaledHeight;
  167.   
  168.    [sourceImage drawInRect:thumbnailRect];
  169.   
  170.    newImage = UIGraphicsGetImageFromCurrentImageContext();
  171.    UIGraphicsEndImageContext();
  172.   
  173.    if(newImage == nil) NSLog(@"could not scale image");
  174.   
  175.   
  176.    return newImage ;
  177. }
  178. - (UIImage *)imageRotatedByRadians:(CGFloat)radians
  179. {
  180.    return [self imageRotatedByDegrees:RadiansToDegrees(radians)];
  181. }
  182. - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
  183. {  
  184.    // calculate the size of the rotated view's containing box for our drawing space
  185.    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
  186.    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
  187.    rotatedViewBox.transform = t;
  188.    CGSize rotatedSize = rotatedViewBox.frame.size;
  189.    [rotatedViewBox release];
  190.   
  191.    // Create the bitmap context
  192.    UIGraphicsBeginImageContext(rotatedSize);
  193.    CGContextRef bitmap = UIGraphicsGetCurrentContext();
  194.   
  195.    // Move the origin to the middle of the image so we will rotate and scale around the center.
  196.    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
  197.   
  198.    //   // Rotate the image context
  199.    CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
  200.   
  201.    // Now, draw the rotated/scaled image into the context
  202.    CGContextScaleCTM(bitmap, 1.0, -1.0);
  203.    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
  204.   
  205.    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  206.    UIGraphicsEndImageContext();
  207.    return newImage;
  208.   
  209. }
  210. @end;
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值