0

I want to manipulate image and shuffle colors. I'm trying to rotate 180 degress with pixels but failed. I don't want to use UIImageView rotate cause i won't just rotate images. I want to do them whatever i want.

EDIT : It was wrong operator. I dont know why i used % instead of / . Anyways i hope this code helps someone(it works).

- (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

2 Answers2

0

Assuming that you are wanting to make the image turn upside down (rotate it 180) and not mirror it, I found some relevant code on another question that may help you:

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

If you're trying to mirror the image, this code example from this question maybe of help:

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

UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
                                            scale:sourceImage.scale
                                      orientation:UIImageOrientationUpMirrored]; 
Community
  • 1
  • 1
tww0003
  • 801
  • 9
  • 18
  • i wanna do something like this but do this with pixels one by one. –  Dec 01 '16 at 15:54
  • Admittedly, I'm not very familiar with that process, but [this link](https://developer.apple.com/library/content/qa/qa1509/_index.html) should help with getting the pixel data from the CGImage. After you get the pixel data, – tww0003 Dec 01 '16 at 16:31
  • Oops, sent the comment before I was finished typing it and I'm not sure how to edit it... Anyways, after you get the pixel data, you'll want to perform a [reflection](https://en.wikipedia.org/wiki/Reflection_(mathematics)) upon that data, which should mirror it. – tww0003 Dec 01 '16 at 16:33
  • i did same thing with that link but still don't know why second photo is one color. –  Dec 02 '16 at 13:30
0

So you're looking to actually manipulate the raw pixel data. Check this out then:

Getting the pixel data from a CGImage object

It's for MacOS but should be relevant for iOS as well.

cbiggin
  • 1,942
  • 1
  • 17
  • 18
  • i did same thing with that link but still don't know why second photo is one color. –  Dec 02 '16 at 13:29