首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CGContextDrawImage?

如何使用CGContextDrawImage?
EN

Stack Overflow用户
提问于 2013-03-22 13:29:17
回答 2查看 7.9K关注 0票数 2

我需要一些使用CGContextDrawImage的帮助。我有以下代码,它将创建一个位图上下文,并将像素数据转换为CGImageRef。现在,我需要使用CGContextDrawImage显示该图像。我不是很清楚我应该如何使用它。以下是我的代码:

代码语言:javascript
复制
- (void)drawBufferWidth:(int)width height:(int)height pixels:(unsigned char*)pixels
    {
     const int area = width *height;
     const int componentsPerPixel = 4;
     unsigned char pixelData[area * componentsPerPixel];

     for(int i = 0; i<area; i++)
     {
          const int offset = i * componentsPerPixel;
          pixelData[offset] = pixels[0];
          pixelData[offset+1] = pixels[1];
          pixelData[offset+2] = pixels[2];
          pixelData[offset+3] = pixels[3];
     }

     const size_t BitsPerComponent = 8;
     const size_t BytesPerRow=((BitsPerComponent * width) / 8) * componentsPerPixel;
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
     CGContextRef gtx = CGBitmapContextCreate(pixelData, width, height, BitsPerComponent, BytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
     CGImageRef myimage = CGBitmapContextCreateImage(gtx);

     //What code should go here to display the image?

     CGContextRelease(gtx);
     CGImageRelease(myimage);

}

任何帮助或一段示例代码都会很棒。提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-22 13:49:04

创建名为MyDrawingView.h的文件

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface MyDrawingView : UIView
{
}

@end

现在创建一个名为MyDrawingView.m的文件

代码语言:javascript
复制
#import "MyChartView.h"

@implementation MyDrawingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        // Write your initialization code if any.

    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    // Create Current Context To Draw
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIImage *image = [UIImage imageNamed:@"image.jpg"];
    // Draws your image at given poing
    [image drawAtPoint:CGPointMake(10, 10)];
}

//现在在您的视图中使用它

代码语言:javascript
复制
 #import "MyChartView.h"    

 MyDrawingView *drawView = [[MyDrawingView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
 [self.view addSubview:drawView];


 // whenever you want to update that view call
 [drawView setNeedsDisplay];

//这是你处理(绘制)图像的方法

代码语言:javascript
复制
- (void)drawBufferWidth:(int)width height:(int)height pixels:(unsigned char*)pixels
{
    const int area = width *height;
    const int componentsPerPixel = 4;
    unsigned char pixelData[area * componentsPerPixel];

    for(int i = 0; i<area; i++)
    {
        const int offset = i * componentsPerPixel;
        pixelData[offset] = pixels[0];
        pixelData[offset+1] = pixels[1];
        pixelData[offset+2] = pixels[2];
        pixelData[offset+3] = pixels[3];
    }

    const size_t BitsPerComponent = 8;
    const size_t BytesPerRow=((BitsPerComponent * width) / 8) * componentsPerPixel;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef gtx = CGBitmapContextCreate(pixelData, width, height, BitsPerComponent, BytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
    CGImageRef myimage = CGBitmapContextCreateImage(gtx);

    // Convert to UIImage
    UIImage *image = [UIImage imageWithCGImage:myimage];

    // Create a rect to display
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

    // Here is the Two Snnipets to draw image
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Transform image
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Finaly Draw your image
    CGContextDrawImage(context, imageRect, image.CGImage);

    // You can also use following to draw your image in 'drawRect' method
    //    [[UIImage imageWithCGImage:myimage] drawInRect:CGRectMake(0, 0, 145, 15)];

    CGContextRelease(gtx);
    CGImageRelease(myimage);
}
票数 1
EN

Stack Overflow用户

发布于 2015-05-31 23:50:27

如果有其他人处理这个问题,请尝试将以下代码插入到Ereka的代码中。Dipen的解决方案似乎有点过了。在注释“//什么代码应该在这里显示图像”之后,放入以下代码:

代码语言:javascript
复制
CGRect myContextRect = CGRectMake (0, 0, width, height);
CGContextDrawImage (gtx, myContextRect, myimage);
CGImageRef imageRef = CGBitmapContextCreateImage(gtx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
UIImageView *imgView = [[UIImageView alloc] initWithImage:finalImage];
[self.view addSubview:imgView];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15563343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档