2014-01-31 2 views
0

iOS에서 여러 UIImages를 병합하고 싶습니다. 이 작업을 수행하기 위해, 나는 다음과 같이하려고 노력 :iOS에서 여러 UIImages 병합

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]; 

하지만 모든 이미지를 얻을 수 없습니다. 어떻게 해결할 수 있습니까? 감사합니다. .

+0

'를 UIImage imageLeft * = [있는 UIImage imageNamed "ICO-left.png"@]'또는'를 UIImage imageLeft * = [있는 UIImage imageNamed : @ "ICO-left.jpg"] 무엇 ' – vikingosegundo

+0

당신은'어떤 이미지도 얻을 수 없다 '는 뜻입니까? 디버깅을하면 어떻게됩니까? – Wain

답변

0

나는 당신이 이미지 (PNG로)의 확장을 추가 그래서이 문제인지 확실하지 오전이

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

확장자가 .png 인 경우 추가하는 것은 필요하지 않습니다. .jpg 또는 .jpeg 인 경우 확장 프로그램을 추가해야합니다. –

+0

이것은 좋은 대답이 아닙니다. 이미지 이름에 .png를 추가하지 말아야하며 특히 이미지에 확장명이없는 xcassets 이미지 카탈로그를 사용하려는 경우. – Climbatize

0

하여 위 두 줄을 변경하지만 당신은 그것을 시도를 제공 할 수 없습니다 생각합니다. 때로는 저에게 도움이되었습니다

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]; 
0

다음은 이미지를 병합하는 데 사용하는 코드입니다. (나는 어떤 점에서 그것의 전부 또는 일부를 온라인으로 찾았다 고 생각한다). 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; 

    }