合成图片

  • 直接合成图片还是比较简单的,现在的难点是要把,通过文本输入的一些基本数据也合成到一张图片中,如果有多长图片就合成长图。
  • 现在的实现方法是,把所有的文本消息格式化,然后绘制到一个UILable中,然后自适应高度,然后把这个控件截取出来一张图片,和拍的照片合成一张图片。

示例界面如下

  • 1、基本信息截图

  • 2、一张图片

  • 3、两张图片

  • 4、三张图片

具体代码

  • 首先初始化界面
/// 初始化子控件
- (void)setupViews {
//
_nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, 22, ScreenWidth, 44)];
_nameField.placeholder = @"请输入姓名";
[self.view addSubview:_nameField]; _ageField = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_nameField.frame), ScreenWidth, 44)];
_ageField.placeholder = @"请输入年龄";
[self.view addSubview:_ageField]; _infoField = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_ageField.frame), ScreenWidth, 44)];
_infoField.placeholder = @"请输入简介";
[self.view addSubview:_infoField]; _timeField = [[UITextField alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(_infoField.frame),ScreenWidth, 44)];
_timeField.text = [self getCurrentDate];
_timeField.enabled = NO;
[self.view addSubview:_timeField]; _collectionView = [[SLQCollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_timeField.frame),ScreenWidth, 100)];
[_collectionView setTitle:@"相关照片"];
__weak typeof (self)weakSelf = self;
_collectionView.heightAndPhotosBlock = ^(CGFloat height,NSArray *photos){
[weakSelf.photoArr removeAllObjects];
weakSelf.photoArr = [NSMutableArray arrayWithArray:photos];
}; [self.view addSubview:_collectionView]; _mergePhoto = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_collectionView.frame), 100, 44)];
[_mergePhoto setTitle:@"发布" forState:UIControlStateNormal];
_mergePhoto.backgroundColor = [UIColor redColor];
[_mergePhoto addTarget:self action:@selector(postPhoto) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_mergePhoto]; _mergePhoto.center = CGPointMake(ScreenWidth/2, _mergePhoto.center.y); _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
_contentLabel.hidden = YES; [self.view addSubview:_contentLabel]; }
  • 发布图片
/// 发布图片
- (void)postPhoto {
self.postImage = nil; NSString *name = self.nameField.text;
NSString *age = self.ageField.text;
NSString *info = self.infoField.text;
NSString *time = self.timeField.text; NSString *content = [NSString stringWithFormat:@"姓名:%@\n年龄:%@\n简介:%@\n时间:%@\n相关图片:",name,age,info,time];
self.contentLabel.numberOfLines = 0;
self.contentLabel.text = content;
[self.contentLabel sizeToFit];
self.contentLabel.hidden = NO;
[self.contentLabel setNeedsDisplay]; if (self.photoArr.count) {
for (NSInteger i = 0 ; i < self.photoArr.count; i ++) { self.postImage = [self mergeImages:self.photoArr[i]];
}
}
}
  • 合成图片
// 获得顶部图片
- (UIImage *)getImageFromView
{
// 已经合成过一次,就去上次的合成结果
if(self.postImage) {
return self.postImage;
}else
{
UIGraphicsBeginImageContextWithOptions(self.contentLabel.frame.size, NO, 0.0);
//获取图像
[self.contentLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.contentLabel.hidden = YES;
// 保存图片,需要转换成二进制数据
[self saveImageToPhotos:image];
self.contentLabel.hidden = YES;
self.textImage = image;
return image;
}
} // 获得待合成图片
- (UIImage *)mergeImages:(UIImage *)mergeImage
{
UIImage *newimage = mergeImage;
UIImage *postImage = [self getImageFromView];
// 获取位图上下文
UIGraphicsBeginImageContextWithOptions(CGSizeMake(ScreenWidth, postImage.size.height + ScreenHeight - self.textImage.size.height), NO, 0.0);
[newimage drawInRect:CGRectMake(0, postImage.size.height, ScreenWidth, ScreenHeight - self.textImage.size.height)]; [postImage drawAtPoint:CGPointMake(0,0)];
// 获取位图
UIImage *saveimage = UIGraphicsGetImageFromCurrentImageContext();
// 关闭位图上下文
UIGraphicsEndImageContext();
// 保存图片,需要转换成二进制数据
[self saveImageToPhotos:saveimage];
return saveimage;
}
- (void)saveImageToPhotos:(UIImage*)savedImage
{
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
} // 指定回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
message:msg
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
  • 属性声明
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width #import "ViewController.h"
#import "SLQCollectionView.h" @interface ViewController ()
/// UILabel
@property (nonatomic, strong) UILabel *contentLabel;
/// UITextField
@property (nonatomic, strong) UITextField *nameField;
/// UITextField
@property (nonatomic, strong) UITextField *ageField;
/// UITextField
@property (nonatomic, strong) UITextField *infoField;
/// UITextField
@property (nonatomic, strong) UITextField *timeField;
/// SLQCollectionView
@property (nonatomic, strong) SLQCollectionView *collectionView;
/// SLQCollectionView
@property (nonatomic, strong) UIButton *mergePhoto;
/// 图片数组
@property (nonatomic, strong) NSMutableArray *photoArr; /// 文字图片
@property (nonatomic, strong) UIImage *textImage;
/// 将要保存的图片
@property (nonatomic, strong) UIImage *postImage;
@end

总结

  • 合成长图原来也这么简单,哈哈

iOS-合成图片(长图)的更多相关文章

  1. android 不失真 显示 超高清 图片 长图

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 通过计算 位图工厂.选项  对象的 inSamleSize 值 等比压缩 图片. 使用 ...

  2. iOS - 长按图片识别图中二维码

    长按图片识别图中二维码: // 长按图片识别二维码 UILongPressGestureRecognizer *QrCodeTap = [[UILongPressGestureRecognizer a ...

  3. ios中,长按Webview中的图片

    我们所要解决的问题如题目所示:ios中,长按Webview中的图片,将图片保存到本地相册. 解决方案:对load的html网页,执行js注入,通过在webview中执行js代码,来响应点击事件,通过j ...

  4. ios开发之滑动长图截全屏应用

    最近做项目遇到要求截取图片长度超出手机屏幕,即可滑动的长图截屏,这里简单说一下解决思路,下面附带Demo下载地址. ,当我们要截全屏时,将滑动视图的frame以及偏移量记录下来,然后将滑动视图偏移量设 ...

  5. Vue实现长按图片识别图中二维码

    Vue实现长按图片识别图中二维码 思路:要想实现可以识别图片中的二维码,那必定是要将这张图进行上传操作,上传则需要file对象格式.不管是在H5还是APP中,展示的图片都是通过url的方式展示在img ...

  6. 一个iOS图片选择器的DEMO(实现图片添加,宫格排列,图片长按删除,以及图片替换等功能)

    在开发中,经常用到选择多张图片进行上传或作其他处理等等,以下DEMO满足了此功能中的大部分功能,可直接使用到项目中. 主要功能如下: 1,图片九宫格排列(可自动设置) 2,图片长按抖动(仿苹果软件删除 ...

  7. 谈谈 iOS 中图片的解压缩

    原文 对于大多数 iOS 应用来说,图片往往是最占用手机内存的资源之一,同时也是不可或缺的组成部分.将一张图片从磁盘中加载出来,并最终显示到屏幕上,中间其实经过了一系列复杂的处理过程,其中就包括了对图 ...

  8. 【转】谈谈 iOS 中图片的解压缩

    转自:http://blog.leichunfeng.com/blog/2017/02/20/talking-about-the-decompression-of-the-image-in-ios/ ...

  9. web实时长图实践--摘抄

    背景简介 全民K歌专辑发布新玩法,传统宣传专辑战绩的流程,从获取数据,到制作海报,到传播,周期长运营成本高,如何快速分享战绩进行荣誉感的传播成为一个亟待解决的问题. 产品:能不能在专辑大事件触发时,自 ...

随机推荐

  1. LA 4987 背包

    题意: 有n个施工队,给定他们的位置,有m个防空洞,给定位置,求将施工队放到m个防空洞里面,最少的总距离? n<=4000 分析: dp[i][j] 前 i 个施工队,放到前 j 个防空洞里面的 ...

  2. POJ 3020 Antenna Placement 【最小边覆盖】

    传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. Ubuntu 16.04 安装 IDEA

    1.下载地址:https://www.jetbrains.com/idea/download/#section=linux 选择without jdk版本下载 2.下载完成 解压 到 /opt下 先却 ...

  4. SPFA 全面讲解

    SPFA全面讲解 --最短路高效算法 标签: 最短路 简介:SPFA 是1994年在西安交通大学段凡丁同学所提出,是将Dijsktra以及Bellman-Ford两种最短路算法完美结合的一个算法,效率 ...

  5. linux学习(一)开始

    第一关 用u盘安装ubuntu, 大部份工作制作的安装U盘会失败,使用Win32DiskImager就行了,这个工具需要手动填写完整iso路径. 第二个问题 装完后发现乱码,连英文都乱码,不知道原因, ...

  6. 今天升级win10.vs调试程序各种崩溃

    今天升级win10.vs调试程序各种崩溃.感觉代码没问题.崩溃时有时没有.不知道是win10的问题,好真是我的代码问题. 问题1: 尝试读取或写入受保护的内存.这通常指示其他内存已损坏 不过.当我写这 ...

  7. swiper插件使用技巧

    1.加载插件: <!DOCTYPE html> <html> <head> ... <link rel="stylesheet" href ...

  8. 构建Linux根文件系统(未完待续)

          所谓制作根文件系统, 就是创建各种目录, 并且在里面创建各种文件. 比如在/bin ./sbin 目录下存放各种可执行程序, 在/etc 目录下存放配置文件, 在/lib 目录下存放库文件 ...

  9. c#的二进制序列化组件MessagePack介绍

    c#的序列化有多种,我一般喜欢用第三方组件,一个公共组件要拿出来用,而且支持很多语言,甚至以此谋生,肯定有其优势. 有或者说存在必然有其合理性,经过几年开发,我更加喜欢第三方的东西,类似序列化的东西. ...

  10. oracle 12.1的删除和创建公共用户问题

    版本12.1 ,cdb-rac模式 一个集群,4个节点,一个cdb,下面有12个pdb. os:linux 64 --- 删除用户 drop user c##test atler.log提示 ORA- ...