Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法向UITableViewCell中的UIImage添加阴影_Iphone_Objective C_Cocoa Touch_Uitableview_Calayer - Fatal编程技术网

Iphone 无法向UITableViewCell中的UIImage添加阴影

Iphone 无法向UITableViewCell中的UIImage添加阴影,iphone,objective-c,cocoa-touch,uitableview,calayer,Iphone,Objective C,Cocoa Touch,Uitableview,Calayer,我有一个UITableViewCell,它正在drawContentView:中绘制图像。我想给它添加一个阴影,所以我使用CALayers: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; AsyncCell *cell = (Asy

我有一个UITableViewCell,它正在
drawContentView:
中绘制图像。我想给它添加一个阴影,所以我使用CALayers:

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

    AsyncCell *cell = (AsyncCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[AsyncCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary* obj = [facebookPhotosData objectAtIndex:indexPath.row];
    [cell updateCellInfo:obj];

    CALayer *sublayer = [CALayer layer];
    sublayer.contents = (id)cell.image.CGImage;
    sublayer.shadowOffset = CGSizeMake(0, 3);
    sublayer.shadowRadius = 5.0;
    sublayer.shadowColor = [UIColor blackColor].CGColor;
    sublayer.shadowOpacity = 0.8;
    sublayer.frame = CGRectMake(5.0, 5.0, cell.image.size.width, cell.image.size.height);
    [cell.layer addSublayer:sublayer];

    return cell;
}
这是我的drawContentView方法,如果我在这里添加子层,它们将继续堆积:

- (void) drawContentView:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    CGContextFillRect(context, rect);

    NSString* caption = [NSString stringWithFormat:@"%@",[info objectForKey:@"caption"]];
    NSString* text = [info stringForKey:@"text"];

    CGFloat widthr = self.frame.size.width - 70;

    [[UIColor grayColor] set];
    [text drawInRect:CGRectMake(63.0, 25.0, widthr, 20.0) withFont:system14 lineBreakMode:UILineBreakModeTailTruncation];

    if (self.image) {
        UIImage *imageToDisplay;
        imageToDisplay = self.image;
        imageToDisplay = [self imageWithImage:imageToDisplay scaledToSize:CGSizeMake(imageToDisplay.size.width / 1.5, imageToDisplay.size.height / 1.5)];
        CGFloat width;
        CGFloat height;
        CGRect r;
        if (imageToDisplay.size.width < 310 && imageToDisplay.size.height > 290) {
            imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(0, 20, imageToDisplay.size.width, 250)];

        }
        else if (imageToDisplay.size.width > 310 && imageToDisplay.size.height < 20) {
            imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, 250)];
        }
        else {
            imageToDisplay = [self imageByCropping:imageToDisplay toRect:CGRectMake(30, 0, 290, 250)];

        }

        width = imageToDisplay.size.width;
        height = imageToDisplay.size.height;
        r = CGRectMake(5.0, 5.0, width, height);

        [imageToDisplay drawInRect:r];

        CALayer *sublayer = [CALayer layer];
        sublayer.contents = (id)imageToDisplay.CGImage;
        sublayer.shadowOffset = CGSizeMake(0, 3);
        sublayer.shadowRadius = 5.0;
        sublayer.shadowColor = [UIColor blackColor].CGColor;
        sublayer.shadowOpacity = 0.8;
        sublayer.frame = CGRectMake(5.0, 5.0, imageToDisplay.size.width, imageToDisplay.size.height);
        [self.layer addSublayer:sublayer];


        //Experimental shadow stuff with images
        /*CALayer *layer = [CALayer layer];
        layer = [CALayer layer];
        layer.bounds = CGRectMake(5.0, 5.0, imageToDisplay.size.width, imageToDisplay.size.height);
        layer.position = CGPointMake(150, 140);
        layer.contents = (id)imageToDisplay.CGImage;    

        layer.shadowOffset = CGSizeMake(0, 2);
        layer.shadowOpacity = 0.70;

        [self.layer addSublayer:layer];

        [self bezierPathWithCurvedShadowForRect:layer.bounds];*/

        [[UIColor blackColor] set];
        [caption drawInRect:CGRectMake(0, 270 , 298, 20.0) withFont:system14 lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
    }
}
-(void)drawContentView:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
[[UIColor-whiteColor]set];
CGContextFillRect(上下文,rect);
NSString*caption=[NSString stringWithFormat:@“%@,[info objectForKey:@“caption”];
NSString*text=[info stringForKey:@“text”];
CGFloat widthr=self.frame.size.width-70;
[[UIColor grayColor]set];
[text-drawInRect:CGRectMake(63.0,25.0,widthr,20.0),字体:system14 lineBreakMode:UILineBreakModeTailTruncation];
if(self.image){
UIImage*图像显示;
imageToDisplay=self.image;
imageToDisplay=[自映像带映像:imageToDisplay scaledToSize:CGSizeMake(imageToDisplay.size.width/1.5,imageToDisplay.size.height/1.5)];
CGFloat宽度;
浮子高度;
cGRECTR;
if(imageToDisplay.size.width<310&&imageToDisplay.size.height>290){
imageToDisplay=[self-imageByCropping:imageToDisplay-toRect:CGRectMake(0,20,imageToDisplay.size.width,250)];
}
否则,如果(imageToDisplay.size.width>310&&imageToDisplay.size.height<20){
imageToDisplay=[self-imageByCropping:imageToDisplay-toRect:CGRectMake(30,0290250)];
}
否则{
imageToDisplay=[self-imageByCropping:imageToDisplay-toRect:CGRectMake(30,0290250)];
}
宽度=imageToDisplay.size.width;
高度=imageToDisplay.size.height;
r=CGRectMake(5.0,5.0,宽度,高度);
[ImageToDisplayDrawinRect:r];
CALayer*子层=[CALayer层];
sublayer.contents=(id)imagestodisplay.CGImage;
sublayer.shadowOffset=CGSizeMake(0,3);
子层阴影半径=5.0;
sublayer.shadowColor=[UIColor blackColor].CGColor;
sublayer.shadowOpacity=0.8;
sublayer.frame=CGRectMake(5.0,5.0,imageToDisplay.size.width,imageToDisplay.size.height);
[self.layer addSublayer:sublayer];
//实验用阴影填充图像
/*CALayer*层=[CALayer层];
层=[CALayer层];
layer.bounds=CGRectMake(5.0,5.0,imageToDisplay.size.width,imageToDisplay.size.height);
layer.position=CGPointMake(150140);
layer.contents=(id)imageToDisplay.CGImage;
layer.shadowOffset=CGSizeMake(0,2);
layer.shadowOpacity=0.70;
[self.layer addSublayer:layer];
[自bezierPathWithCurvedShadowForRect:layer.bounds]*/
[[UIColor blackColor]set];
[标题drawInRect:CGRectMake(0,270,298,20.0),字体:system14 lineBreakMode:UILineBreakMode换行对齐:UITextAlignmentCenter];
}
}

当我将代码放在这个委托方法中时,什么都没有发生。如果我将其添加到drawContentView,它会绘制阴影,但在滚动时不断添加层,这是不正确的。不确定如何将阴影添加到图像中?

根据提供的描述,我不太清楚drawContentView方法中有什么以及何时调用它,但您给出的代码“滚动时不断添加层”的原因是每次调用tableView:cellForRowAtIndexPath:时,它确实会将子层添加到单元格中(滚动时经常发生这种情况)

因此,第一个明显的解决方案是:

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

     AsyncCell *cell = (AsyncCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[AsyncCell alloc] initWithStyle:UITableViewCellStyleDefault                 reuseIdentifier:CellIdentifier];
        CALayer *sublayer = [CALayer layer];
        sublayer.contents = (id)cell.image.CGImage;
        sublayer.shadowOffset = CGSizeMake(0, 3);
        sublayer.shadowRadius = 5.0;
        sublayer.shadowColor = [UIColor blackColor].CGColor;
        sublayer.shadowOpacity = 0.8;
        sublayer.frame = CGRectMake(5.0, 5.0, cell.image.size.width, cell.image.size.height);
        [cell.layer addSublayer:sublayer];
    }

    NSDictionary* obj = [facebookPhotosData objectAtIndex:indexPath.row];
    [cell updateCellInfo:obj];

    return cell;
}
因此,您只需要绘制一次阴影,可能是在创建单元时。 就我个人而言,我宁愿将这段代码放在AsyncCell类中。
希望这有帮助。

哦,现在我知道发生了什么。尝试将所有代码从drawContentView:移动到drawRect:方法。这应该可以解决问题,因为drawRect方法仅在设置setNeedsDisplay标志时调用,而不是每次用户滚动时调用。

我得到相同的结果,阴影不会出现。当我将其放入drawContentVi中时ew,它被添加了多次。更新了原始问题。drawContentView在哪里调用?您使用drawInRect而不仅仅是设置cell.textLabel.text属性有什么原因吗?这样做是为了在滚动时加快速度吗?您在drawRect中调用drawContentView方法吗?