带有代码

时间:2017-09-22 11:01:51

标签: ios objective-c iphone uitableview

我有自己的自定义UITableViewCell,我想在单元格类中直接绘制图像。我正在使用此代码:

- (UIImage *)drawImageWithIdentifier:(NSString *)currencyIdentifier inView:(UIImageView *)imageView {

NSLog(@"width: %f, height: %f", imageView.frame.size.width, imageView.frame.size.height);

ColorTable *colorTable = [[ColorTable alloc] init];

CGFloat contentInset = imageView.frame.size.height/4;

UIView *backgroundView = [[UIView alloc] initWithFrame:self.imageView.bounds];
backgroundView.backgroundColor = [colorTable colorWithID:currencyIdentifier];

UIImage *currencyImage = [UIImage imageNamed:@"dollar_icon.png"];

UIGraphicsBeginImageContextWithOptions(backgroundView.bounds.size, NO, [UIScreen mainScreen].scale);

[[UIBezierPath bezierPathWithRoundedRect:backgroundView.bounds cornerRadius:backgroundView.frame.size.height/2] addClip];

[backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];

CGFloat aspectRatio = currencyImage.size.width/currencyImage.size.height;

if (aspectRatio < 1) {
    //vertical image
    CGRect imageRect = CGRectMake(0, 0, (backgroundView.frame.size.height - 2*contentInset) *aspectRatio , backgroundView.frame.size.height - 2*contentInset);
    CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
    CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);

    [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
}
else if (aspectRatio > 1) {
    //horizontal image
    CGRect imageRect = CGRectMake(0, 0, (backgroundView.frame.size.height - 2*contentInset) *aspectRatio, backgroundView.frame.size.height - 2*contentInset);
    CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
    CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);

    [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
}
else if (aspectRatio == 1) {
    //square image
    CGRect imageRect = CGRectMake(0, 0, backgroundView.frame.size.height - 2*contentInset, backgroundView.frame.size.height - 2*contentInset);
    CGFloat pointX = (backgroundView.frame.size.width / 2) - (imageRect.size.width / 2);
    CGFloat pointY = (backgroundView.frame.size.height / 2) - (imageRect.size.height / 2);

    [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];
}

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return finalImage;
}

在常规视图控制器中,它绘制图像时没有任何问题,但是当我将此代码放入单元格类时,我收到类似<Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.的错误

如何以编程方式成功为UITableViewCell绘制图像?

更新

我发现这是我的错误,因为我使用self.imageView.bounds作为backgroundView的尺寸参数。我在IB中设计了我的细胞,我的细胞类使用self.currencyImage作为UIImageView。错误不再显示,但我的self.currencyImage仍为空。

更新2:

我终于发现了我犯的错误: 在我的UITableViewCell的.h文件中,我有一个字符串属性,应该作为- (UIImage *)drawImageWithIdentifier:(NSString *)currencyIdentifier inView:(UIImageView *)imageView传递给currencyIdentifier。但是看起来currencyIdentifier是NULL,尽管我在TableViewController中正在做cell.currencyIdentifier = @"USD";

2 个答案:

答案 0 :(得分:0)

此代码的问题

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [
  {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
      os.path.join(BASE_DIR, '/my_app/templates').replace('\\', '/'),
    ],
    'APP_DIRS': True,
    'OPTIONS': {
      'context_processors': [
        'django.template.context_processors.debug',
        'django.template.context_processors.request',
        'django.contrib.auth.context_processors.auth',
        'django.contrib.messages.context_processors.messages',
      ],
    },
  },
]

不要在UITableViewCell

中使用draw rect

创建图像,然后将其作为子视图添加到单元格

 [currencyImage drawInRect:CGRectMake(pointX, pointY, imageRect.size.width, imageRect.size.height) blendMode:kCGBlendModeNormal alpha:0.1];

答案 1 :(得分:0)

不要在循环中调用DrawRect。其昂贵的方法。

在单独的循环中创建图像将它们添加到数组中并使用它。

static NSString *cellIdentifier = @"Cell";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

 if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

[cell addSubView:[self drawImageWithIdentifier:@"Identifier" inView:cell.contentView]];

 return cell