关于将叠加图像与捕获图像ios合并的问题

时间:2012-06-29 13:14:54

标签: iphone ios

我正在开发相机应用程序,其中我放置了一个叠加图像,因为我捕获了图像。我正在使用以下代码进行处理以组合图像,但徽标的位置不正确。

- (UIImage*)addOverlayToBaseImage:(UIImage*)baseImage {
     UIImage *newImage = [self CombineImage:baseImage];
     return newImage;
}

-(UIImage *)CombineImage :(UIImage*)myCapturedImage{ 

  CGSize finalSize = [myCapturedImage size];
  CGSize overlaySize = [overlayGraphic size];
  UIGraphicsBeginImageContext(finalSize);

  [myCapturedImage drawInRect:CGRectMake(0,0,finalSize.width,finalSize.height)];

  CGFloat xScaleFactor = finalSize.width / 320;
  CGFloat yScaleFactor = finalSize.height / 480;

  [overlayGraphic drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)]; 

  UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();
  return combinedImage;
 }    

我从此网址[http://www.musicalgeometry.com/?p=1681]获取引用并使用该Capture camera view and onverlay in an image

如果我的代码有任何错误,请指导我。

感谢。

2 个答案:

答案 0 :(得分:0)

试试这个......

- (UIImage*)addOverlayToBaseImage:(UIImage*)baseImage{

UIImage *overlayImage = [UIImage imageNamed:@"img.png"];    
CGPoint topCorner = CGPointMake(0, 0);
CGSize targetSize = CGSizeMake(320, 480);   
CGRect scaledRect = CGRectZero;

CGFloat scaledX = 480 * baseImage.size.width / baseImage.size.height;
CGFloat offsetX = (scaledX - 320) / -2;

scaledRect.origin = CGPointMake(offsetX, 0.0);
scaledRect.size.width  = scaledX;
scaledRect.size.height = 480;

UIGraphicsBeginImageContext(targetSize);    
[baseImage drawInRect:scaledRect];  
[overlayImage drawAtPoint:topCorner];   
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();    

return result;  
}

答案 1 :(得分:0)

请尝试替换此行:

 [overlayGraphic drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor,
 overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)];

这一行

[overlayGraphic drawInRect:CGRectMake(0, 0, finalSize.width, finalSize.Height)];