首页 文章

从CGImageRef创建的UIImage因UIImagePNGRepresentation而失败

提问于
浏览
13

我正在使用以下代码来裁剪并创建一个更大的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 回答

  • 1

    我有同样的问题,但只有在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

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

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

  • 3

    看来你正在尝试的灵魂应该有效,但我建议使用这个:

    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);
    

    希望能帮助到你 .

  • 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];
    

    希望这能帮助你.... :)

相关问题