UIImage (Crop)解决UIImagePNGRepresentation(image)图片问题

#import <UIKit/UIKit.h>


@interface UIImage (Crop)

- (UIImage*) imageByCroppingToRect:(CGRect)rect;

- (UIImage*) imageByCroppingSelf;

@end


#import "UIImage+Crop.h"


@implementation UIImage (Crop)

- (UIImage*) imageByCroppingToRect:(CGRect)rect

{

    //create a context to do our clipping in

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    

    //create a rect with the size we want to crop the image to

    //the X and Y here are zero so we start at the beginning of our

    //newly created context

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

    CGContextClipToRect( currentContext, clippedRect);

    

    //create a rect equivalent to the full size of the image

    //offset the rect by the X and Y we want to start the crop

    //from in order to cut off anything before them

    CGRect drawRect = CGRectMake(rect.origin.x * -1,

                                 rect.origin.y * -1,

                                 self.size.width,

                                 self.size.height);

    

    //draw the image to our clipped context using our offset rect

    CGContextTranslateCTM(currentContext, 0.0, rect.size.height);

    CGContextScaleCTM(currentContext, 1.0, -1.0);

    CGContextDrawImage(currentContext, drawRect, self.CGImage);

    

    //pull the image from our cropped context

    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

    

    //pop the context to get back to the default

    UIGraphicsEndImageContext();

    

    //Note: this is autoreleased

    return cropped;

}

- (UIImage*) imageByCroppingSelf

{

    //create a context to do our clipping in

    UIGraphicsBeginImageContext(self.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    

    //create a rect with the size we want to crop the image to

    //the X and Y here are zero so we start at the beginning of our

    //newly created context

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

    CGContextClipToRect( currentContext, clippedRect);

    

    //create a rect equivalent to the full size of the image

    //offset the rect by the X and Y we want to start the crop

    //from in order to cut off anything before them

    CGRect drawRect = CGRectMake(0,

                                 0,

                                 self.size.width,

                                 self.size.height);

    

    //draw the image to our clipped context using our offset rect

    CGContextTranslateCTM(currentContext, 0.0, self.size.height);

    CGContextScaleCTM(currentContext, 1.0, -1.0);

    CGContextDrawImage(currentContext, drawRect, self.CGImage);

    

    //pull the image from our cropped context

    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

    

    //pop the context to get back to the default

    UIGraphicsEndImageContext();

    

    //Note: this is autoreleased

    return cropped;

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值