您好我正在开发需要合并两个 Image 的应用程序,通过合并两个 Image 我的图像大小为 320*240 我希望大小为 320 * 480 。我怎样才能以编程方式做到这一点。以下是图片
==================================================== ==================================
刚刚对此进行了测试,根据您正在使用的图像的大小创建您的上下文并将它们相互叠加(假设它们的宽度相同):
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];
为避免降低图像质量,请使用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];
斯威夫特版本
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!)
此实现适用于可变参数,因此您可以传递无限数量的图像以垂直合并:
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()
}
您可以使用两个不同的图像视图,一个在另一个之下。并分配两个不同的图像。
斯威夫特 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
}
//添加了 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)
#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();
}
我认为您必须获取 ypur 图像的上下文,然后对其进行标记。
UIImage *eyes = [UIImage imageNamed:@"eyes"];
UIGraphicsBeginImageContext(CGSizeMake(m, n));
CGContextRef context1 = UIGraphicsGetCurrentContext();
UIImage *mouth = [UIImage imageNamed:@"mouth"];
UIGraphicsBeginImageContext(CGSizeMake(m, n));
CGContextRef context2 = UIGraphicsGetCurrentContext();
通过绘制具有不同边界的上下文进行合并
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();