【问题标题】:How to use CGContextDrawImage?如何使用CGContextDrawImage?
【发布时间】:2013-03-22 05:29:16
【问题描述】:

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

- (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);

}

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

【问题讨论】:

    标签: quartz-2d cgcontextdrawimage


    【解决方案1】:

    创建一个名为:MyDrawingView.h 的文件

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    
    @interface MyDrawingView : UIView
    {
    }
    
    @end
    

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

    #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)];
    }
    

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

     #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];
    

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

    - (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);
    }
    

    【讨论】:

    • 非常感谢您的回复!我尝试了代码,没有显示任何内容。也许我错过了一些东西。我需要指定视图还是什么?
    • 创建一个自定义类(UIView 的子视图)以编程方式将其添加到您的视图中,然后在您的自定义类中覆盖“- (void)drawRect:(CGRect)rect” 方法。并在那里写这段代码,试试这个..
    • 如果我只是用图像调用 drawInRect 方法就足够了,对吧?我不必重写drawRect,因为我只显示而不对图像进行任何修改..
    • 绘制你需要的东西只需要使用drawRect方法,无论是它的图像还是任何线条或任何对象。
    • 我放了一个 Ereka 的例子,用你的代码检查一下。我希望你会成功。
    【解决方案2】:

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

    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多