0

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

- (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;
 }    

我从这个 url [ http://www.musicalgeometry.com/?p=1681]中获取参考,并在图像中使用该 onr Capture camera view 和 onverlay

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

谢谢。

4

2 回答 2

0

请尝试替换此行:

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

用这条线

[overlayGraphic drawInRect:CGRectMake(0, 0, finalSize.width, finalSize.Height)];
于 2012-08-08T13:01:54.640 回答
0

Try this...

- (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;  
}
于 2012-08-08T11:36:59.613 回答