Chinaunix首页 | 论坛 | 博客
  • 博客访问: 397216
  • 博文数量: 466
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-16 13:59
文章分类

全部博文(466)

文章存档

2015年(466)

我的朋友

分类: C/C++

2015-03-16 14:33:15

在项目中添加framwork : CoreGraphics.framework然后就可以使用了,我这边有个如下图的 工程样例 需要的画联系我
图片:Picture 1.png
 
 
 
 

- (void)drawLegendsforExerciseChart:(CGContextRef)context xFloat:(float) xPoint yFloat:(float) yPoint {
    // Draw lines for Cycling

    CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
    CGPoint newPoints[] = {
        CGPointMake(xPoint, yPoint),
        CGPointMake( xPoint+20, yPoint),
    };
    CGContextAddLines(context, newPoints, 2);
    CGContextStrokePath(context);
 
    // Draw Cycling legends

    char *commentsMsg;
    UIFont *commentsFont = [UIFont systemFontOfSize:7.0];
    commentsMsg = (char *)[@"Cycling" UTF8String];
    CGSize textSize = [@"Cycling" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Hiking

    CGContextSetStrokeColorWithColor(context, [[UIColor orangeColor] CGColor]);
    CGPoint newPoints1[] = {
        CGPointMake(xPoint, yPoint-10),
        CGPointMake(xPoint+20, yPoint-10),
    };
    CGContextAddLines(context, newPoints1, 2);
    CGContextStrokePath(context);
 
    // Draw Hiking legends

    commentsMsg = (char *)[@"Hiking" UTF8String];
    textSize = [@"Hiking" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-10, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Jogging

    CGContextSetStrokeColorWithColor(context, [[UIColor yellowColor] CGColor]);
    CGPoint newPoints2[] = {
        CGPointMake(xPoint, yPoint-20),
        CGPointMake(xPoint+20, yPoint-20),
    };
    CGContextAddLines(context, newPoints2, 2);
    CGContextStrokePath(context);
 
    // Draw Jogging legends

    commentsMsg = (char *)[@"Jogging" UTF8String];
    textSize = [@"Jogging" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-20, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Rowing

    CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
    CGPoint newPoints3[] = {
        CGPointMake(xPoint, yPoint-30),
        CGPointMake(xPoint+20, yPoint-30),
    };
    CGContextAddLines(context, newPoints3, 2);
    CGContextStrokePath(context);
 
    // Draw Rowing Rowing

    commentsMsg = (char *)[@"Rowing" UTF8String];
    textSize = [@"Rowing" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-30, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Running

    CGContextSetStrokeColorWithColor(context, [[UIColor brownColor] CGColor]);
    CGPoint newPoints4[] = {
        CGPointMake(xPoint, yPoint-40),
        CGPointMake(xPoint+20, yPoint-40),
    };
    CGContextAddLines(context, newPoints4, 2);
    CGContextStrokePath(context);
 
    // Draw Running legends

    commentsMsg = (char *)[@"Running" UTF8String];
    textSize = [@"Running" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-40, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Spinning

    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
    CGPoint newPoints5[] = {
        CGPointMake(xPoint, yPoint-50),
        CGPointMake(xPoint+20, yPoint-50),
    };
    CGContextAddLines(context, newPoints5, 2);
    CGContextStrokePath(context);
 
    // Draw Spinning legends

    commentsMsg = (char *)[@"Hiking" UTF8String];
    textSize = [@"Hiking" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-50, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Swimming

    CGContextSetStrokeColorWithColor(context, [[UIColor purpleColor] CGColor]);
    CGPoint newPoints6[] = {
        CGPointMake(xPoint, yPoint-60),
        CGPointMake(xPoint+20, yPoint-60),
    };
    CGContextAddLines(context, newPoints6, 2);
    CGContextStrokePath(context);
 
    // Draw Swimming legends

    commentsMsg = (char *)[@"Swimming" UTF8String];
    textSize = [@"Swimming" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-60, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for Walking

    CGContextSetStrokeColorWithColor(context, [[UIColor darkGrayColor] CGColor]);
    CGPoint newPoints7[] = {
        CGPointMake(xPoint, yPoint-70),
        CGPointMake(xPoint+20, yPoint-70),
    };
    CGContextAddLines(context, newPoints7, 2);
    CGContextStrokePath(context);
 
    // Draw Walking legends

    commentsMsg = (char *)[@"Walking" UTF8String];
    textSize = [@"Walking" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-70, commentsMsg, strlen(commentsMsg));
 
    // Draw lines for else

    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGPoint newPoints8[] = {
        CGPointMake(xPoint, yPoint-80),        CGPointMake(xPoint+20, yPoint-80),
    };
    CGContextAddLines(context, newPoints8, 2);
    CGContextStrokePath(context);
 
    // Draw else legends

    commentsMsg = (char *)[@"Others" UTF8String];
    textSize = [@"Others" sizeWithFont:commentsFont];
    CGContextSelectFont(context, (char *)[[commentsFont fontName] UTF8String], textSize.height, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextShowTextAtPoint(context, xPoint+25, yPoint-80, commentsMsg, strlen(commentsMsg));
}
 


 

在cocoa下使用NSImage可以通过bitmapImageRep来读取相应的data,在iphone下使用UIImage看到有相关CGImageRef的结构,想要对每一个图像数据进行处理,请教如何使用,有相关sample吗?还请帮忙,谢谢
 
 

void ManipulateImagePixelData(CGImageRef inImage)
{
    // Create the bitmap context

    CGContextRef cgctx = CreateARGBBitmapContext(inImage);
    if (cgctx == NULL)
    {
        // error creating context

        return;
    }
 
     // Get image width, height. We'll use the entire image.

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}};
 
    // Draw the image to the bitmap context. Once we draw, the memory

    // allocated for the context for rendering will then contain the

    // raw image data in the specified color space.

    CGContextDrawImage(cgctx, rect, inImage);
 
    // Now we can get a pointer to the image data associated with the bitmap

    // context.

    void *data = CGBitmapContextGetData (cgctx);
    if (data != NULL)
    {
 
        // **** You have a pointer to the image data ****

 
        // **** Do stuff with the data here ****

 
    }
 
    // When finished, release the context

    CGContextRelease(cgctx);
    // Free image data memory for the context

    if (data)
    {
        free(data);
    }
 
}
 
CGContextRef CreateARGBBitmapContext (CGImageRef inImage)
{
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    void * bitmapData;
    int bitmapByteCount;
    int bitmapBytesPerRow;
 
     // Get image width, height. We'll use the entire image.

    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);
 
    // Declare the number of bytes per row. Each pixel in the bitmap in this

    // example is represented by 4 bytes; 8 bits each of red, green, blue, and

    // alpha.

    bitmapBytesPerRow = (pixelsWide * 4);
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
 
    // Use the generic RGB color space.

    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }
 
    // Allocate memory for image data. This is the destination in memory

    // where any drawing to the bitmap context will be rendered.

    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }
 
    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits

    // per component. Regardless of what the source image format is

    // (CMYK, Grayscale, and so on) it will be converted over to the format

    // specified here by CGBitmapContextCreate.

    context = CGBitmapContextCreate (bitmapData,
                                    pixelsWide,
                                    pixelsHigh,
                                    8, // bits per component

                                    bitmapBytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }
 
    // Make sure and release colorspace before returning

    CGColorSpaceRelease( colorSpace );
 
    return context;
}


,/SPANSPAN style=/SPANSPAN style=
阅读(391) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~