Draw a bitmap picture on IOS based on a set of RGB data

Multi tool use
Draw a bitmap picture on IOS based on a set of RGB data
I'd like to draw a bimap picture on IOS platform based on a set of RGB data but I got some problems when referring to this official web: https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-TPXREF101
The biggest problem here for me is that The tutorial drew a bitmap picture on MAC OS while not on IOS.
SO, I added a subview to the view and intended to draw the bitmap picture on the subview.
UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(15, 355, 384, 288)];
[self drawBitmap:384 andPixelshigh:288];
[self.view addSubview:imgview];
The drawBitmap function is a self-written function used to draw the bitmap following the tutorial.
- (void)drawBitmap:(NSInteger)pixelsWide andPixelshigh: (NSInteger)pixelsHigh{
CGRect myBoundingBox;
CGContextRef myBitmapContext;
CGImageRef myImage;
myBoundingBox = CGRectMake (33, 366, 384, 288);
myBitmapContext = [self MyCreateBitmapContext:pixelsWide andPixelsHigh:pixelsHigh];
CGContextSetRGBFillColor (myBitmapContext, 1, 0, 0, 1);
CGContextFillRect (myBitmapContext, CGRectMake (100, 100, 200, 100 ));
CGContextSetRGBFillColor (myBitmapContext, 0, 0, 1, .5);
CGContextFillRect (myBitmapContext, CGRectMake (100, 100, 100, 200 ));
myImage = CGBitmapContextCreateImage (myBitmapContext);
CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);
char *bitmapData = CGBitmapContextGetData(myBitmapContext);
CGContextRelease (myBitmapContext);
if (bitmapData)
free(bitmapData);
CGImageRelease(myImage);
}
and myBitmapContext is the context of the bitmap picture generated by the following function.
- (CGContextRef) MyCreateBitmapContext: (NSInteger) pixelsWide andPixelsHigh: (NSInteger) pixelsHigh{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
NSInteger * bitmapData;
NSInteger bitmapByteCount;
NSInteger bitmapBytesPerRow;
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
bitmapData = (NSInteger *)calloc( bitmapByteCount, sizeof(uint8_t) );
if (bitmapData == NULL)
{
return NULL;
}
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
if (context== NULL)
{
free (bitmapData);
return NULL;
}
CGColorSpaceRelease( colorSpace );
return context;
}
I think there's nothing wrong with my code but I just can't draw the bitmap figure I want.
2 Answers
2
How about UIImage(data: imageData)
in swift or [UIImage imageWithData:imageData]
in Objective-C
UIImage(data: imageData)
[UIImage imageWithData:imageData]
谢谢你,我犯了个低级错误。。。
– Nanton Zhang
Jul 5 at 15:07
I suddenly realized that I made a very stupid mistake. The function CGBitmapContextCreateImage()
has its return value, which is actually a UIImage
. So the solution to this problem is just to assign the return value to myView.image
to show the image.
CGBitmapContextCreateImage()
UIImage
myView.image
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
A picture is just a set of data. If you can get the data representing a picture, creating an image is trivial. What part of this puzzle are you having trouble with? Your question is lacking any details.
– rmaddy
Jul 3 at 2:23