我有同樣的問題,但只有在iOS 3.2上測試兼容性時。在4.2上它工作正常。
最後我發現這個http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/這兩個工作,雖然有點冗長!
我上的UIImage轉換成一個類別這樣:
的UIImage + Crop.h
@interface UIImage (Crop)
- (UIImage*) imageByCroppingToRect:(CGRect)rect;
@end
的UIImage + Crop.m
@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;
}
@end
當我啓用鏈接時間優化時,我得到了「libpng錯誤:沒有寫入文件的IDAT」。你啓用了這個選項嗎?禁用它修復了我。 – rasmus 2012-06-23 11:03:54