您好我正在开发需要合并两个图像的应用程序,我的图像大小是320 * 240,通过合并两个图像我希望大小为320 * 480。我怎么能这样做呢?以下是图片
=============================================== ====================================
答案 0 :(得分:35)
刚刚对此进行了测试,根据您正在使用的图像的大小创建上下文并将它们相互重叠绘制(假设它们的宽度相同):
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();
//Add image to view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[self.view addSubview:imageView];
答案 1 :(得分:14)
为避免降低图像质量,请使用UIGraphicsBeginImageContextWithOptions。
如果指定值0.0,则比例因子将设置为比例 设备主屏幕的因素。
UIImage *image1 = [UIImage imageNamed:@"image1.png"];
UIImage *image2 = [UIImage imageNamed:@"image2.png"];
CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);
UIGraphicsBeginImageContextWithOptions(size, false, 0.0) // Use this call
[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();
//Add image to view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
imageView.image = finalImage;
[self.view addSubview:imageView];
答案 2 :(得分:7)
Swift版本
func getMixedImg(image1: UIImage, image2: UIImage) -> UIImage {
var 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))
var finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
您可以将图片调用此函数,如下所示:
var myimage1 = UIImage(named: "image1.png")
var myimage2 = UIImage(named: "image2.png")
var finalMixedImage = getMixedImg(myimage1!, image2: myimage2!)
答案 3 :(得分:2)
您可以使用两个不同的图像视图,并分配两个不同的图像。
答案 4 :(得分:1)
//添加Swift 3.2和4.0
@IBOutlet weak var imgCamera1: UIImageView!
@IBOutlet weak var imgCamera2: UIImageView!
let image1 = imgCamera1.image
let image2 = imgCamera2.image
let size = CGSize(width: image1?.size.width ?? 0.0, height: (image1?.size.height)! + (image2?.size.height)!)
UIGraphicsBeginImageContext(size)
image1?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: image1?.size.height ?? 0.0))
image2?.draw(in: CGRect(x: 0, y: image1?.size.height ?? 0.0, width: size.width, height: image2?.size.height ?? 0.0))
let finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Add image to view
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: finalImage?.size.width ?? 0.0, height: finalImage?.size.height ?? 0.0))
imageView.image = finalImage
//Displaying Image
// view.addSubview(imageView)
答案 5 :(得分:0)
UIImage *Image1 = [[UIImage alloc] initWithData:data];
UIImage *Image2 = [[UIImage alloc] initWithData:data];
// Set up width height with values.
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
[Image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[Image2 drawInRect:CGRectMake(newSize.width,newSize.height,newSize.width,newSize.height*2) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
答案 6 :(得分:0)
我认为你必须得到你的图像的上下文然后合并它。
UIImage *eyes = [UIImage imageNamed:@"eyes"];
UIGraphicsBeginImageContext(CGSizeMake(m, n));
CGContextRef context1 = UIGraphicsGetCurrentContext();
UIImage *mouth = [UIImage imageNamed:@"mouth"];
UIGraphicsBeginImageContext(CGSizeMake(m, n));
CGContextRef context2 = UIGraphicsGetCurrentContext();
通过绘制具有不同边界的上下文进行合并
答案 7 :(得分:0)
#pragma mark - merging two images
-(void)mergeimage :(UIImage*)firstImage withImage:(UIImage*)secondImage{
CGSize size = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));
UIGraphicsBeginImageContext(size);
[firstImage drawInRect:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,size.width/2,size.height)];
[secondImage drawInRect:CGRectMake(self.view.frame.origin.x+(size.width/2),self.view.frame.origin.y,size.width/2,size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
答案 8 :(得分:0)
斯威夫特3:
接受图像数组并将图像的图像一个返回到另一个上面 考虑到最大尺寸,因此它是无可救药的
func combineTopToBottom(imagesArray:[UIImage]) -> UIImage? {
var dimensions = CGSize(width: 0.0, height: 0.0)
for image in imagesArray {
dimensions.width = max(dimensions.width, image!.size.width)
dimensions.height += max(dimensions.height, image!.size.height)
}
UIGraphicsBeginImageContext(dimensions)
var lastY = CGFloat(0.0)
for image in imagesArray {
image?.draw(in:CGRect(x: 0, y: lastY, width: dimensions.width, height: image!.size.height))
lastY += image!.size.height
}
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
答案 9 :(得分:0)
此实现适用于可变参数,因此您可以传递无限数量的图像以进行垂直合并:
public static func mergeVertically(images: UIImage...) -> UIImage? {
let maxWidth = images.reduce(0.0) { max($0, $1.size.width) }
let totalHeight = images.reduce(0.0) { $0 + $1.size.height }
UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: totalHeight), false, 0.0)
defer {
UIGraphicsEndImageContext()
}
let _ = images.reduce(CGFloat(0.0)) {
$1.draw(in: CGRect(origin: CGPoint(x: 0.0, y: $0), size: $1.size))
return $0 + $1.size.height
}
return UIGraphicsGetImageFromCurrentImageContext()
}