2016-12-01 8 views
0

画像とシャッフルの色を操作したい。ピクセルで180度の回転をしようとしていますが、失敗しました。私はUIImageViewを使用したくない回転私は単に画像を回転されません。私は何でもしたいと思います。目的Cの画像操作

EDIT:それは間違った演算子でした。私はなぜ/の代わりに%を使ったのかわかりません。とにかく私はこのコードが誰か(それは動作します)を助けることを望みます。

- (IBAction)shuffleImage:(id)sender { 

[self calculateRGBAsAndChangePixels:self.imageView.image atX:0 andY:0]; 

} 

-(void)calculateRGBAsAndChangePixels:(UIImage*)image atX:(int)x andY:(int)y 

{ 


NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * image.size.width; 
NSUInteger bitsPerComponent = 8; 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bmContext = CGBitmapContextCreate(NULL, image.size.width, image.size.height, bitsPerComponent,bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 
CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, image.size.width, image.size.height}, image.CGImage); 

UInt8* data = (UInt8*)CGBitmapContextGetData(bmContext); 

const size_t bitmapByteCount = bytesPerRow * image.size.height; 
NSMutableArray *reds = [[NSMutableArray alloc] init]; 
NSMutableArray *greens = [[NSMutableArray alloc] init]; 
NSMutableArray *blues = [[NSMutableArray alloc] init]; 



for (size_t i = 0; i < bitmapByteCount; i += 4) 
{ 
    [reds addObject:[NSNumber numberWithInt:data[i]]]; 
    [greens addObject:[NSNumber numberWithInt:data[i+1]]]; 
    [blues addObject:[NSNumber numberWithInt:data[i+2]]]; 

} 

for (size_t i = 0; i < bitmapByteCount; i += 4) 
{ 

    data[i] = [[reds objectAtIndex:reds.count-i%4-1] integerValue]; 
    data[i+1] = [[greens objectAtIndex:greens.count-i%4-1] integerValue]; 
    data[i+2] = [[blues objectAtIndex:blues.count-i%4-1] integerValue]; 

} 

CGImageRef newImage = CGBitmapContextCreateImage(bmContext); 
UIImage *imageView = [[UIImage alloc] initWithCGImage:newImage]; 
self.imageView.image = imageView; 
} 

first photo shuffle photo

答えて

0

あなたがイメージがそれをミラー裏返しにする(180それを回転させる)していない作りたいと思っていると仮定すると、私はあなたを助けるかもしれないanother questionにいくつかの関連するコードが見つかりました:

static inline double radians (double degrees) {return degrees * M_PI/180;} 
UIImage* rotate(UIImage* src, UIImageOrientation orientation) 
{ 
    UIGraphicsBeginImageContext(src.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orientation == UIImageOrientationRight) { 
     CGContextRotateCTM (context, radians(90)); 
    } else if (orientation == UIImageOrientationLeft) { 
     CGContextRotateCTM (context, radians(-90)); 
    } else if (orientation == UIImageOrientationDown) { 
     // NOTHING 
    } else if (orientation == UIImageOrientationUp) { 
     CGContextRotateCTM (context, radians(90)); 
    } 

    [src drawAtPoint:CGPointMake(0, 0)]; 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

あなたが助けのかもしれないthis questionから、画像をミラーするために、このコード例をしようとしている場合:

UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"]; 

UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
              scale:sourceImage.scale 
             orientation:UIImageOrientationUpMirrored]; 
+0

このようなことをしたいのですが、これをピクセルごとに1つずつ実行してください。 –

+0

確かに、私はあまりプロセスに慣れていませんが、[このリンク](https://developer.apple.com/library/content/qa/qa1509/_index.html)は、 CGImage。ピクセルデータを取得した後で、 – tww0003

+0

おっと、入力を終える前にコメントを送信しましたが、編集方法はわかりません...とにかく、ピクセルデータを取得したら、[反射](https://en.wikipedia.org/wiki/Reflection_(数学))を参照してください。 – tww0003

0

実際に生のピクセルデータを操作しようとしています。これをチェックしてください:それはMacOSのためだが、同様のiOSに関連する必要があります

Getting the pixel data from a CGImage object

+0

私はそのリンクで同じことをしましたが、2番目の写真がなぜ1色なのかまだ分かりません。 –