如何为imageview添加阴影?

时间:2011-07-29 07:37:08

标签: iphone ipad uiimageview

我正在开发一个应用程序,我有一个imageView通过使用imageView的图像属性添加图像。但在我将图像添加到imageView之前,我想在右侧添加阴影效果的imageView。 我在堆栈溢出中找到了很多答案,我被要求在drawInRect方法中调用代码,这可以在我在imageView上绘制图像时完成。但是我添加图像是为了给它一个丰富的外观,我的图像是最好的质量。

有人可以建议我采取一种方法。

我想要的结束图像看起来像this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier=@"identifier";

    [[ListDataSource sharedDataSource] loadFiles];
    ShelfCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        cell=[[[ShelfCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
    }
    cell.backgroundColor=[UIColor greenColor];
            if (indexPath.row%2==0) {

                cell.imageView1.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+0].titleImage;
                cell.imageView2.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+1].titleImage;
                cell.imageView3.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+2].titleImage;
                cell.imageView4.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+3].titleImage;
                cell.imageView5.image=nil;
            }else {
                cell.rackImage.frame=CGRectMake(128, bookHeight-5, 570, 60);
                cell.imageView1.image=nil;
                cell.imageView2.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+0].titleImage;
                cell.imageView3.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+1].titleImage;
                cell.imageView4.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+2].titleImage;
                cell.imageView5.image=[[ListDataSource sharedDataSource] dataAtIndex:(indexPath.row*4)+3].titleImage;
            }
    return cell;
}


**ShelfCell.m**

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {



        imageView1=[[UIImageView alloc] init];
        imageView1.frame=CGRectMake(10, 15, 110, 150);
        imageView1.layer.masksToBounds=NO;
        imageView1.layer.shadowRadius=5;
        imageView1.layer.shadowOpacity=0.5;
        imageView1.layer.shadowPath=[UIBezierPath bezierPathWithRect:imageView1.frame];
        imageView1.layer.shadowColor = [[UIColor blackColor] CGColor];
        imageView1.layer.shadowOffset = CGSizeMake(5,5);

        [self.contentView addSubview:imageView1];

        imageView2=[[UIImageView alloc] init];
        imageView2.frame=CGRectMake(CGRectGetMaxX(imageView1.frame)+30, 15, 110, 150);

        [self.contentView addSubview:imageView2];


        imageView3=[[UIImageView alloc] init];
        imageView3.frame=CGRectMake(CGRectGetMaxX(imageView2.frame)+30, 15, 110, 150);

        [self.contentView addSubview:imageView3];

        imageView4=[[UIImageView alloc] init];
        imageView4.frame=CGRectMake(CGRectGetMaxX(imageView3.frame)+30, 15, 110, 150);
        [self.contentView addSubview:imageView4];


        imageView5=[[UIImageView alloc] init];
        imageView5.frame=CGRectMake(CGRectGetMaxX(imageView4.frame)+30, 15, 110, 150);
        [self.contentView addSubview:imageView5];

    }
    return self;

}


**ListDataSource**

-(id)dataAtIndex:(NSUInteger)index{

    if (index>=[arrayOfDataDirectories count]) {
        return nil;
    }
    InfoDataSource *info = [[InfoDataSource alloc] init];
    [info loadDirectoryAtPath:[arrayOfDataDirectories objectAtIndex:index]];
    [info autorelease];
    return info;
}

2 个答案:

答案 0 :(得分:2)

首先反对QuartzCore框架,并将#import <QuartzCore/QuartzCore.h>添加到头文件的顶部。

然后假设您的UIImageView被称为yourImageView这可能是出口或本地实例化的视图),您可以将以下内容放入viewDidLoad方法中...

yourImageView.layer.masksToBounds = NO;
yourImageView.layer.shadowOffset = CGSizeMake(5, 0);
yourImageView.layer.shadowRadius = 5;
yourImageView.layer.shadowOpacity = 0.5;
yourImageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:yourImageView.bounds].CGPath;

您不应该尝试覆盖drawRect:子类的UIImageView方法,因为它实际上从未被调用过。引自Apple的文档...

  

特别注意事项

     

优化UIImageView类以将其图像绘制到显示器。 UIImageView不会在子类上调用drawRect:。如果您的子类需要自定义绘图代码,建议您使用UIView作为基类。

答案 1 :(得分:1)

您可以尝试在图像视图的图层中添加阴影,如下所示

imageView.layer.shadowColor = [[UIColor blackColor] CGColor];
imageView.layer.shadowOffset = CGSizeMake(5, 5); // Choose the offset you would like

有更多可用的阴影属性。看看here

此外,在头文件中,您需要#import <QuartzCore/QuartzCore.h>并将QuartzCore库链接到您的项目。如果这对您有用,请告诉我。