在iOS上合并多个UIImage

在iOS上合并多个UIImage,ios,uiimageview,uiimage,Ios,Uiimageview,Uiimage,我想在iOS中合并多个UIImage。 为了做到这一点,我尝试如下: UIImage *imageLeft = [UIImage imageNamed:@"ico-left"]; UIImage *imageRight = [UIImage imageNamed:@"ico-right"]; CGSize size = CGSizeMake(imageLeft.size.width + imageRight.size.width, imageLeft.size.height); UIGraphi

我想在iOS中合并多个UIImage。 为了做到这一点,我尝试如下:

UIImage *imageLeft = [UIImage imageNamed:@"ico-left"];
UIImage *imageRight = [UIImage imageNamed:@"ico-right"];
CGSize size = CGSizeMake(imageLeft.size.width + imageRight.size.width, imageLeft.size.height);
UIGraphicsBeginImageContext(size);
[imageLeft drawInRect:CGRectMake(0, 0, imageLeft.size.width, imageLeft.size.height)];
[imageRight drawInRect:CGRectMake(imageLeft.size.width, 0, imageRight.size.width, imageRight.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[cell.contentView addSubview:imageView];
但我无法得到任何图像。我怎样才能修好它?
谢谢。

我认为您没有添加图像(.png)的扩展名,所以请通过这些更改您的上两行

    UIImage *imageLeft = [UIImage imageNamed:@"ico-left.png"];
    UIImage *imageRight = [UIImage imageNamed:@"ico-right.png"];

我不确定这是否是你的问题,但你可以试一试。有时候对我有用

NSString *path1 = [[NSBundle mainBundle] pathForResource:@"ico-left" ofType:@"png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"ico-right" ofType:@"png"];
UIImage *imageLeft = [[UIImage alloc] initWithContentsOfFile:path1];
UIImage *imageRight = [[UIImage alloc] initWithContentsOfFile:path2];

下面是我用来合并图像的代码。(我想我在网上找到了它的全部或部分)。将其放入UIImage类别中

    // NOTE! this method should only be called from the main thread because of
    // UIGraphicsGetImageFromCurrentImageContext();
    - (UIImage *)merge:(UIImage *)image atRect:(CGRect)pos overlay:(BOOL)overlay fillColor:(UIColor *)fill 
    {
        UIGraphicsBeginImageContext(self.size);

        UIImage *bottom = (overlay)?self:image;
        UIImage *top = (overlay)?image:self;

        CGRect lf = CGRectMake(0, 0, self.size.width, self.size.height);

        CGRect bottomRect = (overlay)?lf:pos;
        CGRect topRect = (overlay)?pos:lf;

        if (fill){
            [fill setFill];
            CGContextFillRect (UIGraphicsGetCurrentContext(), topRect);
        }

        [bottom drawInRect:bottomRect];
        [top drawInRect:topRect];

        UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();    
        UIGraphicsEndImageContext();
        return destImage;   

    }

UIImage*imageLeft=[UIImage imagename:@“ico left.png”]
UIImage*imageLeft=[UIImage-imagename:@“ico left.jpg”]什么意思
无法获取任何图像
?调试时会发生什么?如果扩展名为.png,则无需添加它。如果是.jpg或.jpeg,则必须添加扩展名。这不是一个好的答案,您不应该在图像名称中添加.png,特别是如果您希望在图像没有扩展名的情况下使用xcassets图像目录。