将2张图片合并为一张,就像Instagram一样

时间:2014-03-09 14:10:52

标签: ios xcode

我正在试图找出像Instagram一样的2个图像视图的单个图像坑 这些是2张图片。提前谢谢。

 UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 42.0f, 280.0f, 280.0f)];
[photoImageView setBackgroundColor:[UIColor blackColor]];
[photoImageView setImage:self.image];
[photoImageView setContentMode:UIViewContentModeScaleAspectFit];

//Add overlay
UIImage *overlayGraphic = [UIImage imageNamed:@"chiu"];
UIImageView *overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(30, 100, 260, 200);
[photoImageView addSubview:overlayGraphicView];

1 个答案:

答案 0 :(得分:1)

我不确定您是否可以仅使用UIImageView控件直接执行此操作。我认为你将不得不进入低级别的绘图程序来完成这项任务。



选项1(不推荐,只是尝试回答原始问题):

您是否尝试将叠加UIImageView置于“主”UIImageView之上并将其不透明度设置为小于1(比如0.4)?这是一个粗暴的黑客,但它可能会让你到处。



选项2(可能是更好的旅行路径):

创建图像上下文,然后在其上绘制“基础”和“叠加”图像。然后,您可以输出UIImage,只需要1 UIImageView。这样的事情(注意:这是一个基本的大纲;你需要增加很多时间才能得到你想要的东西!):

UIImage *baseImage = [UIImage imageNamed:@"base"];
UIImage *overlayImage = [UIImage imageNamed:@"overlay"];

UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, 0);

CGContextRef context = UIGraphicsGetCurrentContext();

CGRect rect = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);

CGContextDrawImage(context, rect, baseImage.CGImage);
CGContextDrawImage(context, rect, overlayImage.CGImage);

UIImage *combined = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();