【问题标题】:Merge multiple imageviews合并多个图像视图
【发布时间】:2014-05-02 04:15:26
【问题描述】:

如果我的问题太愚蠢,请原谅我是 iOS 开发的新手。

我有 5 个图像视图。每个图像视图都有不同的背景颜色。

通过单击按钮,我想生成 1 张图像,并将所有 5 张图像组合在一起。含义(1 张图片,5 种颜色)。有人可以告诉我如何实现这一目标吗?

我的故事板看起来与所附图片相似:

【问题讨论】:

  • 一张图片5色怎么排列?您需要提供更多信息,说明您希望最终结果是什么样子。
  • 不可能用 5 种颜色制作 1 个图像视图。请提供更多信息您的要求是什么
  • @Divine 如果您希望图片与图像中显示的完全相同(从左到右),您可以创建一个 5 倍于一个图像大小的图像上下文,并在其位置绘制每个图像。
  • 其实@HAS 我想知道他的要求是什么。意味着将我们必须制作的五种颜色组合成一种颜色,我们必须像饼图一样显示该颜色。所以我问:)
  • 它是 5 种颜色的 5 个图像视图。我想将这 5 个图像视图合并为 1 个。

标签: ios uiimageview


【解决方案1】:

下面的方法非常适合高度相同的图像,我会在有时间的时候为不同高度的图像添加选项。 (更多信息请阅读下面的方法文档)。

该方法被设计为UIImage 的类方法,它留在一个类别中。

你可以这样称呼它

self.generatedImageView.image = [UIImage imageByAppendingImagesSideBySide:@[image1, image2, image3, image4, image5]];

/**
 *  Append images side-by-side. This method does not yet support height adjustments (like vertical align), instead images are aligned at the top.
 *  Example:
 *  Pic1 and Pic2 have both the same width and height.
 *  Pic3 has a larger width (which is fully supported).
 *  Pic4 has a larger height (which is **not** supported (yet)). That means that you cannot align the images as you want - they are aligned at the top.
 *  @code
 *  _______________________________________________________________
 *  |           |           |                 |                   |
 *  |           |           |                 |                   |
 *  |   Pic 1   |   Pic 2   |      Pic 3      |                   |
 *  |           |           |                 |                   |
 *  |___________|___________|_________________|        Pic4       |
 *  |                                         |                   |
 *  |                                         |                   |
 *  |                  Empty                  |                   |
 *  |                                         |                   |
 *  |_________________________________________|___________________|
 *
 *  @endcode
 *  @param images An array of UIImages.
 *
 *  @return An image with a width of the sum of all images' widths and a height of the largest image's height.
 */
+ (UIImage *)imageByAppendingImagesSideBySide:(NSArray *)images {
    // Set initial width to zero (we add it step-by-step in the for loop beneath, and height to the first images height, in the for loop we will figure out the image with the largest height.
    CGSize size = CGSizeMake(0, ((UIImage *)[images firstObject]).size.height);
    for (UIImage *image in images) {
        if ([image isKindOfClass:[UIImage class]]) {
            size.width = size.width + image.size.width;
            if (image.size.height > size.height) {
                size.height = image.size.height;
            }
        }
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    CGPoint currentRightMostPoint = CGPointZero;
    for (UIImage *image in images) {
        if ([image isKindOfClass:[UIImage class]]) {
            [image drawAtPoint:currentRightMostPoint];
            currentRightMostPoint = CGPointMake(currentRightMostPoint.x + image.size.width, currentRightMostPoint.y);
        }
    }
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return finalImage;
}

【讨论】:

  • 我尝试使用它,但它给了我一个错误,说没有方法选择器的类。所以我尝试如下:paste.ubuntu.com/7548957 它给出了一个警告,找不到方法定义。这是整个项目:tempsend.com/04544732C6。你能告诉我我做错了什么吗?
  • generatedImageView.image = [ViewController imageByAppendingImagesSideBySide:@[image1, image2, image3, image4, image5]]; 更改为 generatedImageView.image = [self imageByAppendingImagesSideBySide:@[image1, image2, image3, image4, image5]]; 并将方法名称前的 + 更改为 -。该方法实际上旨在保留在一个类别中,但无论如何您都可以这样使用它;)
  • 没有错误。单击生成按钮时没有任何反应。它应该生成图像并将其设置为 generatedImageView 的背景图像
  • 是的,问题在于您传递的是UIImageViews 数组,而不是UIImages。将数组更改为image1.image 等(使用变量名称image 进行图像视图是个坏主意;))
猜你喜欢
  • 2018-11-18
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多