画像をトリミングして、画像の他の部分を取得したい。以下の画像のように
と
ここでは、画像の選択部分の透明領域を作成し、新しい画像を作成します。
また、すべてのピクセルを取得して、選択部分のアルファを 0 に設定しようとしましたが、うまくいきませんでした。
他の解決策はありますか?
これが私が使用したコードです:
CGSize size = [UIImage imageNamed:fileName].size; CGImageRef inImage = [UIImage imageNamed:fileName].CGImage; CFDataRef ref = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); UInt8 * buf = (UInt8 *) CFDataGetBytePtr(ref); int length = CFDataGetLength(ref); float value2 = (1 + value-0.5); NSLog(@"length = %d",length); int row = 0,col = 0; for(int i=0; i<length; i+=4) { int r = i; int g = i+1; int b = i+2; int a = i+3; col++; if ((col % (int)size.width)==0 ) { row++; col=0; } int red = buf[r]; int green = buf[g]; int blue = buf[b]; int alpha = buf[a]; if (col > 25 && col < 75 && row > 25 && row < 75) { alpha = 0; } buf[r] = SAFECOLOR(red); buf[g] = SAFECOLOR(green); buf[b] = SAFECOLOR(blue); buf[a] = SAFECOLOR(alpha); } NSLog(@"CGImageGetAlphaInfo %d",CGImageGetAlphaInfo(inImage)); NSLog(@"CGImageGetColorSpace %@",CGImageGetColorSpace(inImage)); CGContextRef ctx = CGBitmapContextCreate(buf, CGImageGetWidth(inImage), CGImageGetHeight(inImage), CGImageGetBitsPerComponent(inImage), CGImageGetBytesPerRow(inImage), kCGColorSpaceGenericRGB, kCGImageAlphaPremultipliedLast); CGImageRef img = CGBitmapContextCreateImage(ctx); imgView.image = [UIImage imageWithCGImage:img];