首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableCell图像损坏

TableCell图像损坏
EN

Stack Overflow用户
提问于 2013-10-01 01:17:39
回答 1查看 102关注 0票数 0

我最近一直在做一个mien的小项目,并且尝试使用我在GitHub:https://github.com/nicolasgomollon/NGDynamicGradientCell上找到的这个

不幸的是,正如您在这个Git:https://github.com/aout/TestForCells上看到的那样,我无法使它工作。

我使用它的方式,特别是图像,似乎出了很大问题。没有图像,一切都很好,但是当我添加它们时,我就不能创建任何单元格。

下面是一些代码:

代码语言:javascript
复制
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
    [self setBackgroundColor:[UIColor whiteColor]];

    maskFill = [UIImage imageNamed:@"mask-fill.png"];
    maskFill = [maskFill resizableImageWithCapInsets:UIEdgeInsetsMake(1.0f, 1.0f, 1.0f, 1.0f)];

    bubbleImage = [UIImage imageNamed:@"bubble-min.png"];
    bubbleMaskImage = [UIImage imageNamed:@"mask-bubble-min.png"];
    bubbleEdgeInsetsSent = UIEdgeInsetsMake(17.0f, 20.0f, 17.0f, 26.0f);
    bubbleEdgeInsetsReceived = UIEdgeInsetsMake(17.0f, 26.0f, 17.0f, 20.0f);

    bubbleMaskImage = [bubbleMaskImage resizableImageWithCapInsets:bubbleEdgeInsetsSent];

    grayView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [grayView setBackgroundColor:[UIColor clearColor]];
    [grayView setTintColor:RGBA(229.0f, 229.0f, 234.0f, 1.0f)];
    [grayView setAlpha:0.0f];
    [self.contentView addSubview:grayView];

    messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [messageLabel setBackgroundColor:[UIColor clearColor]];
    [messageLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
    [messageLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [messageLabel setNumberOfLines:0];
    [self.contentView addSubview:messageLabel];
}
return self;
 }

  + (CGFloat)heightForMessage:(NSString *)message
    {
  UIFont *messageFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
  CGSize messageConstraints = CGSizeMake(190.0f, MAXFLOAT);
CGSize messageSize = [message boundingRectWithSize:messageConstraints              options:NSStringDrawingUsesLineFragmentOrigin    attributes:@{NSFontAttributeName : messageFont
     }
  context:nil].size;
CGFloat messageHeight = MAX(35.0f, 10.0f + roundf(messageSize.height) + 5.0f);
return messageHeight + kMessagePadding;
  }

   - (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale);

[image drawInRect:CGRectSetSize(CGRectZero, newSize)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;
  }

  - (void)drawRect:(CGRect)rect {
[super drawRect:rect];

[messageLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
[messageLabel setTextColor:self.sent?[UIColor whiteColor]:[UIColor blackColor]];

CGSize messageConstraints = CGSizeMake(190.0f, MAXFLOAT);
CGSize messageSize = [messageLabel.text boundingRectWithSize:messageConstraints                                                  options:NSStringDrawingUsesLineFragmentOrigin  attributes:@{NSFontAttributeName : messageLabel.font} context:nil].size;

CGFloat originX = 18.0f + kBubbleEdgeInset;
if (self.sent)
    originX = self.bounds.size.width - messageSize.width - originX;

[messageLabel setFrame:CGRectMake(originX, 7.0f, messageSize.width, messageSize.height)];

CGFloat messageWidth = MAX(48.0f, 13.0f + roundf(messageSize.width) + 18.0f);
CGFloat messageHeight = MAX(35.0f, 10.0f + roundf(messageSize.height) + 5.0f);

if (self.sent) {
    UIImage *maskImage = [self imageWithImage:bubbleMaskImage scaledToSize:CGSizeMake(messageWidth, messageHeight)];

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);

    // Draw the bubble mask image.
    [maskImage drawInRect:CGRectMake(self.bounds.size.width - messageWidth - kBubbleEdgeInset, 0.0f, messageWidth, messageHeight)];

    // Draw the message label text (since it'll be masked out).
    [messageLabel.text drawInRect:messageLabel.frame
                   withAttributes:@{NSFontAttributeName : messageLabel.font,
                                    NSForegroundColorAttributeName : messageLabel.textColor}];

    // Draw the mask fill image to the left of the bubble mask image.
    [maskFill drawInRect:CGRectMake(0.0f, 0.0f, self.bounds.size.width - messageWidth - kBubbleEdgeInset, self.bounds.size.height)];

    // Draw the mask fill image to the right of the bubble mask image.
    [maskFill drawInRect:CGRectMake(self.bounds.size.width - kBubbleEdgeInset, 0.0f, kBubbleEdgeInset, self.bounds.size.height)];

    // Draw the mask fill image in the remaining space below the bubble mask image.
    [maskFill drawInRect:CGRectMake(0.0f, messageHeight, self.bounds.size.width, self.bounds.size.height - messageHeight)];

    // Get everything we drew as an image that we can use to mask the cell with.
    UIImage *imageMask = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    CALayer *maskLayer = [CALayer layer];

    [maskLayer setContents:(id)imageMask.CGImage];
    [maskLayer setFrame:self.bounds];

    [self.layer setMask:maskLayer];
} else {
    [grayView setFrame:CGRectMake(kBubbleEdgeInset, 0.0f, messageWidth, messageHeight)];

    UIImage *maskImage = bubbleImage;
    maskImage = [UIImage imageWithCGImage:maskImage.CGImage scale:maskImage.scale orientation:UIImageOrientationUpMirrored];
    maskImage = [maskImage resizableImageWithCapInsets:bubbleEdgeInsetsReceived];
    maskImage = [self imageWithImage:maskImage scaledToSize:CGSizeMake(messageWidth, messageHeight)];
    maskImage = [maskImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

    [grayView setImage:maskImage];
}

[messageLabel setAlpha:!self.sent];
[grayView setAlpha:!self.sent];
}

 - (UILabel *)textLabel {
return messageLabel;
  }

  - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
  }

  @end

有什么帮助吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-18 13:04:32

您可以在这里找到更正:https://github.com/nicolasgomollon/NGDynamicGradientCell/issues/2

两个主要问题:- ARC兼容性要关闭-显示更新要做

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19106454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档