2010-02-10 93 views
13

我使用下面的代码来裁剪并创建一个更大的新UIImage。我已经将CGImageCreateWithImageInRect()函数与CGImage属性设置为不符合我想要的方式来解决问题。 :-)问题是调用函数UIImagePNGRepresentation()失败返回一个零。从CGImageRef创建的UIImage失败,UIImagePNGRepresentation

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

...

NSData *data = UIImagePNGRepresentation (imgCrop); 

- libpng的错误:没有IDATs写入文件

任何想法可能错误或替代用于裁剪一个矩形出来的UIImage的?非常感谢!

+0

当我启用链接时间优化时,我得到了“libpng错误:没有写入文件的IDAT”。你启用了这个选项吗?禁用它修复了我。 – rasmus 2012-06-23 11:03:54

回答

1

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

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

+0

谢谢你的回答。 stillView是UIImageView。我用gdb p/po命令偷看cgCrop和imgCrop的属性和函数返回值,并且两者似乎都有正确的尺寸,imgCrop CGImage引用指向cgCrop等。嗯。下班后会有更详细的外观。 – 2010-02-11 08:48:04

1

它似乎你应该工作的soultion,但我建议使用此:

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

如果您的原始图像是“非常大”,我想知道是否有一个选项将使用原始图像,无论您需要哪个子图像,但在绘制之前设置剪贴蒙版。这将允许您避免从原始图像创建“复制”。这种方法可能有它自己的问题,所以你必须“比较和对比”这两种方法。 – nielsbot 2011-05-02 05:11:18

+0

好 - 我提议的基本上是什么提出的。我认为sicario的方法可能会使用更多的内存。 – nielsbot 2011-05-02 05:12:52

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 
+0

此机制在裁剪时以外的情况下可能非常有用。我有一个UIImage,大概是因为已经在一个不寻常的色彩空间中定义的,并没有被UIImagePNGRepresentation所接受。我使用的代码非常类似于上面的代码,以UIImagePNGRepresentation可接受的形式重新生成UIImage。 – 2016-04-06 08:42:23

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

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