开发者

Draw image from a buffer in iOS

开发者 https://www.devze.com 2023-03-02 11:53 出处:网络
I\'m currently working on an iOS project which is a sort of a client VNC. The server-client connection is successfully 开发者_运维问答established. The program receives the screen image in RAW format a

I'm currently working on an iOS project which is a sort of a client VNC. The server-client connection is successfully 开发者_运维问答established. The program receives the screen image in RAW format and stocks it in a buffer.

My question is how can I draw the buffer in a UIView so that when de buffer is updated so does the view.

I've read the OpenGL ES Programming Guide for iOS

I suppose this is the solution of my problem but I'm really confused. Can someone gives me an example about how to render a buffer in a UIView ? Or if someone has a better solution ?


RemoteScreenController.m
- (void) setMappedPtr:(unsigned char *)ptr {

    m_mappedPtr = ptr;
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * 1680;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, m_mappedPtr, 1680*1050*4, NULL);
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();      
    CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipLast;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

    CGImageRef imageRef = CGImageCreate( 1680, 1050, bitsPerComponent, bitsPerPixel, 
                                                                                bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    myView = [[UIImageView alloc] initWithImage:myImage];
    [self.view addSubview:myView];

In my data manager class I have a reference to RemoteScreenController. When the first buffer is received, it calls setMappedPtr to initialize the image and add it to the view. Then the buffer is updated by another class.


Check out CGImageCreate(), which can then be used to create a proper UIImage ([UIImage imageWithCGImage:cgImage]) from your raw data. Then create a UIImageView and add it to a UIView for display.

For OpenGL, a CGImage should be all you need (as in this example).


Finally I create a CGDataProviderRef and a 'CGImageRef' every time when my buffer is updated, then with [imageView setImage:[UIImage imageWithCGImage:imageRef]]; I redraw the image manually. I'm quite sure there's better solutions, I'm open for all discussions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号