13

我正在使用以下代码从更大的 UIImage 中裁剪并创建一个新的 UIImage。我已经将问题与函数 CGImageCreateWithImageInRect() 隔离开来,它似乎没有按照我想要的方式设置一些 CGImage 属性。:-) 问题是对函数 UIImagePNGRepresentation() 的调用失败返回 nil。

CGImageRef origRef = [stillView.image CGImage];
CGImageRef cgCrop = CGImageCreateWithImageInRect( origRef, theRect);
UIImage *imgCrop = [UIImage imageWithCGImage:cgCrop];

...

NSData *data = UIImagePNGRepresentation ( imgCrop);

-- libpng 错误:没有将 IDAT 写入文件

知道从 UIImage 中裁剪矩形可能有什么问题或替代方法吗?

4

4 回答 4

3

我有同样的问题,但仅在 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+裁剪.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
于 2011-03-17T13:16:55.077 回答
1

在PNG中存在各种块,一些包含调色板信息,一些实际图像数据和一些其他信息,这是一个非常有趣的标准。IDAT 块是实际包含图像数据的位。如果没有“IDAT 写入文件”,则 libpng 从输入数据创建 PNG 时遇到问题。

我不确切知道您的 stillView.image 是什么,但是当您将代码传递给肯定有效的 CGImageRef 时会发生什么?theRect中的实际值是多少?如果您的 theRect 超出了图像的范围,那么您尝试用来制作 UIImage 的 cgCrop 很容易为 nil - 或不为 nil,但不包含图像或宽度和高度为 0 的图像,从而使 libpng 无法工作和。

于 2010-02-10T21:58:43.733 回答
1

看来您正在尝试的解决方案应该可以工作,但我建议使用这个:

CGImageRef image = [stillView.image CGImage];
CGRect cropZone;

size_t cWitdh = cropZone.size.width;
size_t cHeight = cropZone.size.height;
size_t bitsPerComponent = CGImageGetBitsPerComponent(image);
size_t bytesPerRow = CGImageGetBytesPerRow(image) / CGImageGetWidth(image) * cWidth;
        
//Now we build a Context with those dimensions.
CGContextRef context = CGBitmapContextCreate(nil, cWitdh, cHeight, bitsPerComponent, bytesPerRow, CGColorSpaceCreateDeviceRGB(), CGImageGetBitmapInfo(image));

CGContextDrawImage(context, cropZone, image);

CGImageRef result  = CGBitmapContextCreateImage(context);
UIImage * cropUIImage = [[UIImage alloc] initWithCGImage:tmp];

CGContextRelease(context);
CGImageRelease(mergeResult);
NSData * imgData = UIImagePNGRepresentation ( cropUIImage);
于 2010-07-09T15:51:27.393 回答
0
UIImage *croppedImage = [self imageByCropping:yourImageView.image toRect:heredefineyourRect];
 
    CGSize size = CGSizeMake(croppedImage.size.height, croppedImage.size.width);
    UIGraphicsBeginImageContext(size);
    
    CGPoint pointImg1 = CGPointMake(0,0);
    [croppedImage drawAtPoint:pointImg1 ];

    [[UIImage imageNamed:yourImagenameDefine] drawInRect:CGRectMake(0,532, 150,80) ];//here define your Reactangle
    
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    croppedImage = result;
    yourCropImageView.image=croppedImage;
    [yourCropImageView.image retain];
于 2012-05-26T13:26:53.337 回答