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+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
于 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 に答える