如何将2个图像合并/合并为1个
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将2个图像合并/合并为1个相关的知识,希望对你有一定的参考价值。
我想知道如何将2张图像保存到1张图像中。
其中一张照片可以移动,旋转和放大/缩小......
我这样做,但它基本上捕获了屏幕上的所有内容,包括我的按钮......
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *savedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案
- 创建用于添加图像的子视图。
- 在该视图中添加所有图像而不是主视图。
- 让按钮和其他东西留在主视图上。
- 在位图上下文中仅使用图像渲染视图而不是像现在正在执行的主视图。
另一答案
您可以创建图形上下文并在其中绘制两个图像。您将从两个源图像组合中获得图像结果。
- (UIImage*)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
UIImage *image = nil;
CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
} else {
UIGraphicsBeginImageContext(newImageSize);
}
[firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2),
roundf((newImageSize.height-firstImage.size.height)/2))];
[secondImage drawAtPoint:CGPointMake(roundf((newImageSize.width-secondImage.size.width)/2),
roundf((newImageSize.height-secondImage.size.height)/2))];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
另一答案
// Objective-C
UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];
CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);
UIGraphicsBeginImageContext(size);
[image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
[image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//set finalImage to IBOulet UIImageView
imageView.image = finalImage;
//斯威夫特
let topImage = UIImage(named: "image1.png")
let bottomImage = UIImage(named: "image2.png")
let size = CGSize(width: topImage!.size.width, height: topImage!.size.height + bottomImage!.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
topImage!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: topImage!.size.height))
bottomImage!.draw(in: CGRect(x: 0, y: topImage!.size.height, width: size.width, height: bottomImage!.size.height))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
//set finalImage to IBOulet UIImageView
mergeImage.image = newImage
另一答案
您可以使用此方法,该方法非常动态,您可以指定第二个图像的起始位置和图像的总大小。
-(UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect withImageWidth:(int) width
{
CGSize size = CGSizeMake(width,40);
UIGraphicsBeginImageContext(size);
CGPoint pointImg1 = CGPointMake(0,0);
[img drawAtPoint:pointImg1];
CGPoint pointImg2 = cropRect.origin;
[img2 drawAtPoint: pointImg2];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
另一答案
斯威夫特3
在此示例中,使用20%边距的insetBy在另一个图像内绘制frontImage。
必须先按顺序绘制背景图像,然后依次绘制正面图像。
我使用它在UIImageView内部的视频帧图像前面放置一个“播放”图标图像,如下所示:
用法:
self.image = self.mergedImageWith(frontImage: UIImage.init(named: "play.png"), backgroundImage: UIImage.init(named: "backgroundImage.png")))
方法:
func mergedImageWith(frontImage:UIImage?, backgroundImage: UIImage?) -> UIImage{
if (backgroundImage == nil) {
return frontImage!
}
let size = self.frame.size
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
backgroundImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
frontImage?.draw(in: CGRect.init(x: 0, y: 0, width: size.width, height: size.height).insetBy(dx: size.width * 0.2, dy: size.height * 0.2))
let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
另一答案
这是UIImage扩展组合多个图像的方法:
class func combine(images: UIImage...) -> UIImage {
var contextSize = CGSizeZero
for image in images {
contextSize.width = max(contextSize.width, image.size.width)
contextSize.height = max(contextSize.height, image.size.height)
}
UIGraphicsBeginImageContextWithOptions(contextSize, false, UIScreen.mainScreen().scale)
for image in images {
let originX = (contextSize.width - image.size.width) / 2
let originY = (contextSize.height - image.size.height) / 2
image.drawInRect(CGRectMake(originX, originY, image.size.width, image.size.height))
}
let combinedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return combinedImage
}
例:
UIImage.combine(image1, image2)
另一答案
Swift-3(ios10.3)
extension UIImage {
func combineWith(image: UIImage) -> UIImage {
let size = CGSize(width: self.size.width, height: self.size.height + image.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
self.draw(in: CGRect(x:0 , y: 0, width: size.width, height: self.size.height))
image.draw(in: CGRect(x: 0, y: self.size.height, width: size.width, height: image.size.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
}
用法
let image1 = UIImage(named: "image1.jpg")
let image2 = UIImage(named: "image2.jpg")
yourImageView.image = image1?.combineWith(image: image2)
另一答案
基于Himanshu padia的回应
如果你想“动态”将更多图像(具有相同的宽高比)组合到Objective-C
中的一个网格中
在偶数/奇数时隙我只使用了两个这个例子。
等于内联和轮廓边框的公式:
// psx = (x - (n+1)*bs / n)
// psy = (y - (m+1)*bs / m)
不同内联和轮廓边框的公式
// psx = (x - (n-1)*bs + obs / n)
// psy = (y - (m-1)*bs + obs / m)
说明:
- psx,psy - 片段大小X和y
- x,y - 原始(片)图像大小
- n,m - 网格中的插槽
- bs - 边框大小
- obs - 轮廓边框大小
- 为什么n + 1?因为三件,你需要4个边框
|*|*|*|
- 为什么n-1?因为同上,但不包括第一个和最后一个边界
!*|*|*!
码:
UIImage *image1 = [UIImage imageNamed:@"14x9blue"];
UIImage *image2 = [UIImage imageNamed:@"14x9red"];
// grid parameters
int k =0;
int m=3,n = 3;
int i=0, j=0;
CGFloat borderSize = 20.0f;
// equal border inline and outline
// the 1 is a multiplier for easier and more dynamic sizes
// 0*borderSize is inline border only, 1 is equal, 2 is double, etc.
CGFloat outlineBorder = 1*borderSize;
CGSize size = CGSizeMake(self.gridImageView.image.size.width, self.gridImageView.image.size.height);
CGRect gridImage = CGRectMake(0,0, size.width, size.height);
// piece size
// border inline and outline
// psx = (x - (n-1)*bs + obs / n)
// psy = (y - (m-1)*bs + obs / m)
CGFloat pieceSizeX = (size.width - (n-1)*borderSize - 2*outlineBorder) / n;
CGFloat pieceSizeY = (size.height - (m-1)*borderSize - 2*outlineBorder) / m;
UIGraphicsBeginImageContext(size);
// semi transparent fill
[[UIColor colorWithDisplayP3Red:240 green:250 blue:0 alpha:0.5] setFill];
UIRectFill(CGRectMake(0,0, size.width, size.height));
UIImage *currentImage;
for(i=0; i<m; i++) {
for (j=0; j<n; j++) {
if (k++%2) {
curren以上是关于如何将2个图像合并/合并为1个的主要内容,如果未能解决你的问题,请参考以下文章
Qt:如何将 2 个 QQuickItems 合并为一个,然后将其保存为 png
如何将从输入字段获取的画布名称与Javascript中的画布合并?