i am developing a app where i am having a imageView to add a image to it by using the image property of the imageView.but before i add a image to the imageView ,i wanted to add a shadow effect on the right side of the imageView. i found many answers in stack overflow where i was asked to call the code in drawInRect method which can be done when i draw the image on the imageView.But i am adding the image in order to give it a rich look and my images are of best quality.
Can someone suggest me a approach.
the end image i wanted looks like
- (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(CGRect开发者_如何学JAVAGetMaxX(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;
}
First link against the QuartzCore framework, and add #import <QuartzCore/QuartzCore.h>
to the top of your header file.
Then assuming your UIImageView
is called yourImageView
(this could be an outlet or a locally instantiated view) you can put the following in your viewDidLoad
method ...
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;
You shouldn't try to override the drawRect:
method of a UIImageView
subclass as it's never actually called. Quoted from Apple's docs ...
Special Considerations
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: on a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
You can try adding a shadow to the layer of the image view like this
imageView.layer.shadowColor = [[UIColor blackColor] CGColor];
imageView.layer.shadowOffset = CGSizeMake(5, 5); // Choose the offset you would like
There are more shadow properties available. Take a look here.
Also, in your header file you will need to #import <QuartzCore/QuartzCore.h>
and link the QuartzCore library to your project. Let me know if this works for you.
精彩评论