Wednesday, February 22, 2012

Give Gradient effect to Rectangle using CGContextRef

-(void) createImage{

UIGraphicsBeginImageContext(imgView.frame.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGGradientRef gradient;
CGColorSpaceRef colorSpace;

size_t num_locations = 2;
CGFloat locations[3] = { 0.0 , 1.0 };
CGFloat components[12] = { 96.0/256, 135.0/256.0, 51.0/256.0, 1.0, // Start color
190.0/256, 217.0/256.0, 124.0/256.0, 1.0 //Middle color
}; // End color

colorSpace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColorComponents(colorSpace, components,locations, num_locations);

CGPoint startPoint = CGPointMake(180,0);//left side of rect
CGPoint endPoint = CGPointMake(200,0);//right side of rect

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);

//release
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);

imgView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

}

Make Rounded Corner of the View

In your iPhone application to make your rounded you can use this code

CALayer * l = [viewChangePieces layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];

you need to add #import in your header file.