【问题标题】:how to add a shadow to a imageview?如何为图像视图添加阴影?
【发布时间】:2011-07-29 07:37:08
【问题描述】:

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

有人可以建议我一个方法吗?

我想要的最终图像看起来像

- (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;
}

【问题讨论】:

    标签: iphone ipad uiimageview


    【解决方案1】:

    针对 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;
    

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

    特别注意事项

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

    【讨论】:

    • micpringle- 我相应地包括了以上所有内容,但没有奏效。你还有其他方法吗?
    • 我需要更多地了解您是如何设置的,并且您使用的代码我知道上面的代码在我直接从我当前的项目中提取时有效。
    • 我将三个图像放在一个表格视图单元格中,并且在每个图像下我想要有阴影效果。我已经拍摄了 3 个图像视图并将它们放在单元格中。我将您的代码包含在单元格中的索引行中在我将图像添加到图像视图之前的委托方法。
    • 您能否使用添加UIImageViews的代码更新您的原始问题
    • 您能否也发布一张单元格在表格视图中显示时的外观截图?
    【解决方案2】:

    您可以尝试像这样向图像视图的图层添加阴影

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

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

    此外,在您的头文件中,您需要#import <QuartzCore/QuartzCore.h> 并将 QuartzCore 库链接到您的项目。让我知道这是否适合您。

    【讨论】:

    • tnx 回复但我很抱歉说它对我不起作用。我尝试更改偏移量,但仍然没有成功
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2016-04-30
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多