Reputation: 25
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];
}
Upvotes: 2
Views: 624
Reputation: 26395
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.
Upvotes: 1