2 bluetooth99 bluetooth99 于 2013.03.18 10:29 提问

iphone关于图片的剪裁问题

用下面的代码剪裁图片:

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
 {
     CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
     // or use the UIImage wherever you like
     UIImage * img =  [UIImage imageWithCGImage:imageRef];
     CGImageRelease(imageRef);
     return img;
 }

在模拟器中运行的好好的,但是到了设备上就出问题了。

2个回答

redCoral_
redCoral_   2013.03.18 14:27
已采纳

创建一个UIImage:

@implementation UIImage (Crop)

- (UIImage *)crop:(CGRect)cropRect {

    cropRect = CGRectMake(cropRect.origin.x*self.scale, 
                      cropRect.origin.y*self.scale, 
                      cropRect.size.width*self.scale, 
                      cropRect.size.height*self.scale);       

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect);
    UIImage *result = [UIImage imageWithCGImage:imageRef 
                                          scale:self.scale 
                                    orientation:self.imageOrientation]; 
    CGImageRelease(imageRef);
    return result;
}
lianlianbushell
lianlianbushell   2013.03.18 14:25

试试:

- (UIImage *)cropImage:(UIImage *)oldImage {
        CGSize imageSize = oldImage.size;
        UIGraphicsBeginImageContextWithOptions(CGSizeMake( imageSize.width,imageSize.height - 150),NO,0.);
        [oldImage drawAtPoint:CGPointMake( 0, -80) blendMode:kCGBlendModeCopy alpha:1.];
        UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return croppedImage;
}
Csdn user default icon
上传中...
上传图片
插入图片