从CGImageRef创建的UIImage因UIImagePNGRepresentation而失败

时间:2010-02-10 21:13:21

标签: iphone uiimage nsdata cgimage

我正在使用以下代码裁剪并从更大的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 个答案:

答案 0 :(得分: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 + 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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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);

希望它有所帮助。

答案 3 :(得分: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];

希望这可以帮助你.... :)