从CGImageRef创建的UIImage因UIImagePNGRepresentation而失败 [英] UIImage created from CGImageRef fails with UIImagePNGRepresentation

查看:420
本文介绍了从CGImageRef创建的UIImage因UIImagePNGRepresentation而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the following code to crop and create a new UIImage out of a bigger one. I've isolated the issue to be with the function CGImageCreateWithImageInRect() which seem to not set some CGImage property the way I want. :-) The problem is that a call to function UIImagePNGRepresentation() fails returning a nil.

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

...

NSData *data = UIImagePNGRepresentation ( imgCrop);

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

-- libpng error: No IDATs written into file

知道从UIImage中裁剪矩形可能有什么错误或替代方法吗?非常感谢!

Any idea what might wrong or alternative for cropping a rect out of UIImage? Many thanks!

推荐答案

我遇到了同样的问题,但仅限于在iOS 3.2上测试兼容性时。在4.2上它工作正常。

I had the same problem, but only when testing compatibility on iOS 3.2. On 4.2 it works fine.

最后我发现这个 http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ 适用于两者,虽然有点冗长!

In the end I found this http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ which works on both, albeit a little more verbose!

我把它转换为UIImage上的一个类别:

I converted this into a category on UIImage:

UIImage + Crop.h

UIImage+Crop.h

@interface UIImage (Crop)
- (UIImage*) imageByCroppingToRect:(CGRect)rect;
@end

UIImage + Crop.m

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

这篇关于从CGImageRef创建的UIImage因UIImagePNGRepresentation而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆