2013-01-31 45 views
4

我正在尝试处理一些直接来自设备摄像头的图像数据。为了理解CGImage结构,我使用了一个缓冲区来创建一个不适合我的图像(我知道我不应该在Obj-C代码中使用C代码,它只是为了理解)。代码如下:以编程方式在iOS中创建渐变图像

int *outBuf = malloc(sizeof(int) * width * height); 
for (int i = 0; i < width * height;i ++) 
{ 
    outBuf[i] = (((float)i)/((float) width * height)) * 255.f; 
} 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

CGContextRef ctx = CGBitmapContextCreate(outBuf, width, height, 8, width * 4, colorSpace, kCGImageAlphaNone); 

CGImageRef img = CGBitmapContextCreateImage(ctx); 
CFRelease(ref); 
CGContextRelease(ctx); 
free(outBuf); 

CGColorSpaceRelease(colorSpace); 

[view.imgView setImage: [UIImage imageWithCGImage:img]]; 

CGImageRelease(img); 

这应该从一开始到最后像素创建渐变,但它导致了一个奇怪的图形:

generated image

哪里做横向间距来自? (我想解决这个问题,我知道有其他的可能性来绘制渐变)在此先感谢。

回答

3

我已经找到了解决办法:

我是用INT初始化缓冲区*。每个int都是4个字节长,但是灰度图只需要每个像素1个字节。更改缓冲区为char *和修改CGBitmapContextCreate参数固定的问题:

CGBitmapContextCreate(outBuf, width, height, 8, width, colorSpace, kCGImageAlphaNone); 

enter image description here

2

有几种方法来实现你想要的。这里有两个例子: 请注意,这只是为了向你展示一个例子,你应该添加参数/颜色/等等来获得你想要的。希望这可以帮助。

1)直接绘制梯度图。如果你希望额外的绘图,也可以在视图实现中调用[super drawRect:rect]。

- (void)drawRect:(CGRect)rect { 
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); 
    if (CGSizeEqualToSize(self.centerOffset, CGSizeZero) == NO) { 
     center.x += self.centerOffset.width; 
     center.y += self.centerOffset.height; 
    } 

    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.0, 1.0 }; 
    CGFloat components[8] = { 0.0, 0.0, 0.0, 0.5, // Start color 
           0.0, 0.0, 0.0, 0.7 }; // End color 

    CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
    CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 
    CGGradientDrawingOptions options = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation; 
    CGFloat endRadius = [UIApplication sharedApplication].keyWindow.bounds.size.height/2; 
    CGContextDrawRadialGradient(currentContext, gradient, center, 50.0f, center, endRadius, options); 

    CGGradientRelease(gradient); 
    CGColorSpaceRelease(rgbColorspace); 
} 

2)使用CAGradient层

UIColor *startEndColour = [UIColor redColor]; 
    UIColor *middleColor = [UIColor blueColor]; 

    NSArray *horizontalGradientColorsArray = [NSArray arrayWithObjects:(id)[startEndColour CGColor], (id)[middleColor CGColor],(id)[middleColor CGColor], (id)[startEndColour CGColor],nil]; 

    UIView *horizontalGradient1View = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.bounds.size.width, 1.f)]; 
    horizontalGradient1View.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; 
    CAGradientLayer *horizontalGradient1 = [CAGradientLayer layer]; 
    horizontalGradient1.frame = horizontalGradient1View.bounds; 
    horizontalGradient1.colors = horizontalGradientColorsArray; 
    horizontalGradient1.startPoint = CGPointMake(0, 0.5); 
    horizontalGradient1.endPoint = CGPointMake(1.0, 0.5); 
    [horizontalGradient1View.layer insertSublayer:horizontalGradient1 atIndex:0]; 
    [self addSubview:horizontalGradient1View]; 
    [horizontalGradient1View release]; 
+0

谢谢您的回答。我意识到这些可能性,但基本上我试图理解为什么代码清单不工作(我需要为CIImage提到的缓冲区) – Lukas