在 iOS 中将图像覆盖在另一个图像上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7313023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Overlay an image over another image in iOS
提问by ming yeow
i am displaying thumbnail images from youtube in my iOS app. which upon click, will go to youtube.
我在我的 iOS 应用程序中显示来自 youtube 的缩略图。单击后,将转到 youtube。
I need a way of overlaying a play button onto those images. What might be the most straightforward way of doing so?
我需要一种将播放按钮叠加到这些图像上的方法。这样做的最直接方法是什么?
Also, the images are remotely loaded onto a table, so performance is a big consideration
此外,图像是远程加载到桌子上的,因此性能是一个重要的考虑因素
回答by Jano
If you are concerned with table scrolling performance, retrieve the thumbnail and paint the play button over it.
如果您关心表格滚动性能,请检索缩略图并在其上绘制播放按钮。
+(UIImage*) drawImage:(UIImage*) fgImage
inImage:(UIImage*) bgImage
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
[bgImage drawInRect:CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height)];
[fgImage drawInRect:CGRectMake( point.x, point.y, fgImage.size.width, fgImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
回答by creative_rd
Swift 2.2 Version
斯威夫特 2.2 版本
static func drawImage(image foreGroundImage:UIImage, inImage backgroundImage:UIImage, atPoint point:CGPoint) -> UIImage{
UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0)
backgroundImage.drawInRect(CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height))
foreGroundImage .drawInRect(CGRectMake(point.x, point.y, foreGroundImage.size.width, foreGroundImage.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
回答by Rohit
Here is what I did. The cameraImg is the image that I am getting from camera and the other three images are static images that I have displayed on the cameraImg. In this case, size defines, the size for the context we are going to start. The images are drawn in the rect defined by DrawInRect method. Make sure to end the context and you're done.
这是我所做的。cameraImg 是我从相机获取的图像,其他三个图像是我在 cameraImg 上显示的静态图像。在这种情况下,size 定义了我们将要开始的上下文的大小。图像在 DrawInRect 方法定义的矩形中绘制。确保结束上下文,你就完成了。
UIImage *cameraImg = image;
UIImage *leftImg = [UIImage imageNamed:@"apple.jpeg"];
UIImage *rightImg = [UIImage imageNamed:@"Cloud.png"];
UIImage *middleImg = [UIImage imageNamed:@"mario.jpeg"];
CGSize size = CGSizeMake(cameraImg.size.width, cameraImg.size.height);
UIGraphicsBeginImageContext(size);
[cameraImg drawInRect:CGRectMake(0, 0, self.view.window.frame.size.width, self.view.window.frame.size.height)];
[leftImg drawInRect:CGRectMake(x, y, width, height)];
[rightImg drawInRect:CGRectMake(x, y, width, height)];
[middleImg drawInRect:CGRectMake(x, y, width, height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[self.view addSubview:imageView];
回答by Hemang
Swift 3.x
斯威夫特 3.x
func drawImage(image foreGroundImage:UIImage, inImage backgroundImage:UIImage, atPoint point:CGPoint) -> UIImage {
UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0)
backgroundImage.draw(in: CGRect.init(x: 0, y: 0, width: backgroundImage.size.width, height: backgroundImage.size.height))
foreGroundImage.draw(in: CGRect.init(x: point.x, y: point.y, width: foreGroundImage.size.width, height: foreGroundImage.size.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}