6

以下は、元のコードがコンパイルされないため、(このサイトから) コピーし、わずかに変更したコードです。エッジ検出のためにバイト配列を操作し、最終的には色を簡単に変更したいと考えていますが、最初に基本的なコードを機能させたいと考えていました。現在、システムはコンパイルおよび実行されます。画面に下手な象が表示されます。画像をタッチすると消えます。ステップスルーすると、imageWithData の結果が 0x0 として表示されます。私はpngとbmpの両方でこれを試しましたが、同じ結果でした

私が間違っていることの手がかりはありますか?!

ImageViewDrawable は次のように定義されます。

@interface ImageViewDrawable : UIImageView

// I am using the following code to initialize this ImageView
ImageViewDrawable * uiv = [[ImageViewDrawable alloc] initWithImage:[UIImage imageNamed:@"ele.png"] ];

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // get and work on pixel data
    NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(self.image.CGImage));
    char* bytes =[pixelData bytes];

    // Take away the red pixel, assuming 32-bit RGBA
    for(int i = 0; i < [pixelData length]; i += 4) {
        bytes[i] = bytes[i]; // red
        bytes[i+1] = bytes[i+1]; // green
        bytes[i+2] = bytes[i+2]; // blue
        bytes[i+3] = bytes[i+3]; // alpha
    }

    // convert pixel back to uiiimage
    NSData* newPixelData = [NSData dataWithBytes:bytes length:[pixelData length]];
    char * bytes2 =[pixelData bytes];   
    UIImage * newImage = [UIImage imageWithData:newPixelData] ; 
    [self setImage: newImage ]; 
}
4

1 に答える 1

4

imageWithData: 任意のデータを取得せず、RGBA 非圧縮テクスチャとして解釈します。認識された画像形式 (JPG、PNG、TIFF など) のバイトを含むデータを取得し、それらを解析して画像を適切に解凍します。

必要なことを行うには、適切に構成された CGContext (行バイト、ピクセル形式など) を作成する必要があります。バッキング ストレージとして既に割り当てられているメモリを使用するか、ストレージを要求してからコピーします。あなたのバイトをそれに入れます。これを行うと、そのコンテキストから UIImage を作成する機能など、CGContext 関連のすべての機能を使用できます。

于 2009-09-13T03:26:19.850 に答える