2

I'm trying to create a black and white image that on the one hand can be displayed in an UIImageView and on the other hand it should be possible to share this image via e-mail or just be saved to camera roll.

I've got a two dimensional array (NSArray containing other NSArrays) which contains a matrix with the NSInteger values 0 and 1 (for a white and a black pixel).

So I just want to place a black pixel for a 1 and a white one for a 0. All other questions I found deal with changing pixels from an existing image. I hope you can help me!

[EDIT]:

Of course, I didn't want someone to do my work. I couldn't figure out how to create a new image and place the pixels as I wanted to at first, so I tried to edit an existing image that only consists of white pixels and change the color of the pixel if necessary. So my code for trying this, but as you can see I had no idea how the change the pixel. I hope that shows that i was trying on my own.

- (UIImage*) createQRCodeWithText:(NSString*) text andErrorCorrectionLevel:(NSInteger) level {
    QRGenerator *qr = [[QRGenerator alloc] init];
    NSArray *matrix = [qr createQRCodeMatrixWithText:text andCorrectionLevel:level];

    UIImage *image_base = [UIImage imageNamed:@"qr_base.png"];
    CGImageRef imageRef = image_base.CGImage;

    for (int row = 0; row < [matrix count]; row++) {
        for (int column = 0; column < [matrix count]; column++) {
            if ([[[matrix objectAtIndex:row] objectAtIndex:column] integerValue] == 1) {
                //set pixel (column, row) black
            }
        }
    }

    return [[UIImage alloc] initWithCGImage:imageRef];
}
Björn Kaiser
  • 9,882
  • 4
  • 37
  • 57
timo
  • 25
  • 4
  • 3
    Are there any methods for creating this image that you have already tried? If so, sharing those, along with their code, will help others help you. From what I assess, you are asking someone else to simply give you the code to accomplish your goal, which is not in the interest of what this site is here for. We're here to help you get to that point, but you have to show that you have made a reasonable effort at figuring out the issue, and a possible solution. Just my two cents to help you get the most of your experience at SO. Cheers. – Anil Dec 30 '13 at 20:27
  • +1 for the edit. That's the kind of information that the community looks for. It gets the ball rolling faster! Thanks. – Anil Dec 30 '13 at 20:50
  • Welcome and good luck on getting this resolved! – Anil Dec 30 '13 at 20:52

1 Answers1

1

I would create the image from scratch using a CGBitmapContext. You can call CGBitmapContextCreate() to allocate the memory for the image. You can then walk through the pixels just like you're doing now, setting them from your array. When you've finished, you can call CGBitmapContextCreateImage() to make a CGImageRef out of it. If you need a UIImage you can call [+UIImage imageWithCGImage:] and pass it the CGImageRef you created above.

user1118321
  • 25,567
  • 4
  • 55
  • 86