ios - 如何在drawRect中的UIImage的一部分上添加alpha层?

标签 ios core-graphics drawrect

完全是新手来实现自定义drawRect方法(和Core Graphics),但这样做是为了提高UITableView的滚动性能。如果我做了任何愚蠢的事情,请告诉我。

在我的单元格中,我有一个 UIImage,我想在它的底部打印图像的标题。但是,为了使标题文本无论图像如何都能清晰显示,我希望在 UIImage 顶部和标题文本下方有一个不透明度为 ~75% 的黑色矩形。

我尝试了以下方法

[self.picture drawAtPoint:point];

[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75] setFill];
UIRectFill(CGRectMake(rect));

但是最终的填充实际上会吃掉 UIImage(请原谅我糟糕的描述),并且稍微透明的填充下方显示的部分是我的 UITableView 的背景...

我想我可以为矩形制作另一个图像,然后将其绘制在self.picture之上但我想知道这是否是一种更简单的使用方法 UIRectFill为了实现这一目标...

如上所述,我对 Core Graphics 完全陌生,因此任何提示将不胜感激。提前致谢!


另外,我还有第二个问题...下载的图像的尺寸(以像素为单位)是其适合的矩形(以点为单位)的两倍,以考虑视网膜显示。然而,即使在 iPhone4 设备上,它现在也已经超出了这个范围...我该如何解决这个问题(也包括 iPhone4 之前的设备?)

最佳答案

我不做太多自定义drawRect的事情,所以我会将这部分问题推迟给其他人,但通常通过将昂贵的计算转移到表格 View 性能问题可以更容易地解决后台队列,然后在后台操作完成时从主队列异步更新单元格。因此,类似于:

首先,为tableview定义一个操作队列属性:

@property (nonatomic, strong) NSOperationQueue *queue;

然后在viewDidLoad中初始化:

self.queue = [[NSOperationQueue alloc] init];
self.queue.maxConcurrentOperationQueue = 4;

然后在cellForRowAtIndexPath中,您可以:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Do the quick, computationally inexpensive stuff first, stuff here.
    // Examples might include setting the labels adding/setting various controls
    // using any images that you might already have cached, clearing any of the
    // image stuff you might be recalculating in the background queue in case you're
    // dealing with a dequeued cell, etc.

    // Now send the slower stuff to the background queue.

    [self.queue addOperationWithBlock:^{

        // Do the slower stuff (like complex image processing) here.
        // If you're doing caching, update the cache here, too.

        // When done with the slow stuff, send the UI update back
        // to the main queue...

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            // see if the cell is still visible, and if so ...

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell)
            {
                // now update the UI back in the main queue
            }
        }];

    }];

    return cell;
}

您可以通过确保将计算量大的内容的结果缓存到 NSCache 之类的东西中来进一步优化这一点,也可能缓存到 Documents 或其他地方,因此您可以优化完成复杂工作的频率并真正优化 UI。

而且,顺便说一句,当您这样做时,您现在可以只拥有 UILabel (使用 backgroundColor 使用该 UIColor 为黑色0.75 alpha)位于 UIImageView 之上,iOS 会为您处理它。尽可能简单。

关于图像分辨率的最后一个问题,您可以:

  • 使用 View 的 contentScaleFactor 来确定您是否正在处理视网膜,并相应地调整缩略图的大小;或
  • 只需使用 imageview 的 UIViewContentModeScaleAspectFillcontentMode 即可确保您的缩略图正确渲染,无论...如果您使用的是小缩略图(甚至是 2x图片),性能总体还不错。

关于ios - 如何在drawRect中的UIImage的一部分上添加alpha层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12824951/

相关文章:

ios - 如何在 iOS 10 中检查电池磨损程度?

iphone - iOS 核心图形 : Stroking with semi-transparent patterns leads to colors corruption

ios - 警告 : Could not load any Objective-C class information using PaintCode's code

cocoa - 将 NSView 渲染到图像 : cacheDisplayInRect draws transparent areas differently from usual drawRect call

ios - 当子类 UIView,Objective C 时,在重绘方法中得到奇怪的结果

ios - Storyboard使用 segue 将数据从 View Controller 传递到选项卡栏

ios - 节点旋转不跟随手指

ios - 如何在 iOS 中使用图像资源

ios - 检测图像 ios 上的黑线

android - 以编程方式绘制多个矩形