您不能使用阴影,但可以在矩形描边中使用渐变:--------
- (void)viewDidLoad
{
self.navigationController.navigationBarHidden=true;
UIImage *image = [UIImage imageNamed:@"Color-Splash-640x960.jpg"];
UIImage *imgWithRect = [self imageByDrawingCircleOnImage:image];
UIImageView *imgView=[[UIImageView alloc] initWithImage:imgWithRect];
[imgView setFrame:self.view.frame];
[self.view addSubview:imgView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image
{
float strokeWidth=10.0f;
UIGraphicsBeginImageContext(image.size);
[image drawAtPoint:CGPointZero];
CGRect rectangle = CGRectMake(60, 100, 200, 200);
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
UIImage *lImg=[self gradientImageWithSize:CGSizeMake(strokeWidth, rectangle.size.height)];
UIImage *tImg=[self gradientImageWithSize:CGSizeMake(rectangle.size.height, strokeWidth)];
UIImage *rImg=[self gradientImageWithSize:CGSizeMake(strokeWidth, rectangle.size.height)];
UIImage *bImg=[self gradientImageWithSize:CGSizeMake(rectangle.size.width, strokeWidth)];
UIGraphicsBeginImageContext(self.view.frame.size);
[retImage drawAtPoint:CGPointMake(0, 0)];
[lImg drawAtPoint:CGPointMake(rectangle.origin.x, rectangle.origin.y)];
[tImg drawAtPoint:CGPointMake(rectangle.origin.x+strokeWidth, rectangle.origin.y)];
[rImg drawAtPoint:CGPointMake(rectangle.origin.x+rectangle.size.width,rectangle.origin.y+strokeWidth)];
[bImg drawAtPoint:CGPointMake(rectangle.origin.x, rectangle.origin.y+rectangle.size.height)];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
return retImage;
}
- (UIImage *)gradientImageWithSize:(CGSize)size
{
CGSize textSize = CGSizeMake(size.width, size.height);
CGFloat width = textSize.width;
CGFloat height = textSize.height;
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 0.0, 1.0, 1.0, 1.0, // Start color
1.0, 1.0, 0.0, 1.0 }; // End color
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGPoint topCenter = CGPointMake(0, 0);
CGPoint bottomCenter = CGPointMake(0, textSize.height);
CGContextDrawLinearGradient(context, glossGradient, topCenter, bottomCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
// pop context
UIGraphicsPopContext();
// get a UIImage from the image context
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
return gradientImage;
}
要显示阴影,您可以在此处设置矩形的不透明度...
CGFloat components[8] = { 0.0, 1.0, 1.0, 0.2, // Start color
1.0, 1.0, 0.0, 0.4 }; // End color