垂直拼接/合成多个图像并保存为一个图像(iOS,objective-c) [英] Stitch/Composite multiple images vertically and save as one image (iOS, objective-c)

查看:568
本文介绍了垂直拼接/合成多个图像并保存为一个图像(iOS,objective-c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助编写一个Objective-c函数,该函数将接收一组UIImages / PNG,并返回/保存所有拼接在一起的图像的高图像。我是新手,所以请放慢速度并放轻松:)

I need help writing an objective-c function that will take in an array of UIImages/PNGs, and return/save one tall image of all the images stitched together in order vertically. I am new to this so please go slow and take it easy :)

我的想法到目前为止:
绘制一个UIView,然后将addSubviews添加到每个人的父母(图片)
然后???

My ideas so far: Draw a UIView, then addSubviews to each one's parent (of the images) and then ???

推荐答案

正常的方法是创建一个位图图像上下文,绘制你的图像在所需的位置,然后从图像上下文中获取图像。

The normal way is to create a bitmap image context, draw your images in at the required position, and then get the image from the image context.

你可以用UIKit做到这一点,这有点容易,但不是线程安全,所以需要在主线程中运行并阻止UI。

You can do this with UIKit, which is somewhat easier, but isn't thread safe, so will need to run in the main thread and will block the UI.

这里有大量示例代码,但如果你想正确理解它,你应该看看UIGraphicsContextBeginImageContext,UIGraphicsGetCurrentContext,UIGraphicsGetImageFromCurrentImageContext和UIImageDrawInRect。不要忘记UIGraphicsPopCurrentContext。

There is loads of example code around for this, but if you want to understand it properly, you should look at UIGraphicsContextBeginImageContext, UIGraphicsGetCurrentContext, UIGraphicsGetImageFromCurrentImageContext and UIImageDrawInRect. Don't forget UIGraphicsPopCurrentContext.

您也可以使用Core Graphics,这是AFAIK,可以安全地在后台线程上使用(我没有崩溃)然而)。效率大致相同,因为UIKit只是在引擎盖下使用CG。
关键词是CGBitmapContextCreate,CGContextDrawImage,CGBitmapContextCreateImage,CGContextTranslateCTM,CGContextScaleCTM和CGContextRelease(核心图形没有ARC)。缩放和翻译是因为CG的右下角有原点而Y向上增加。

You can also do this with Core Graphics, which is AFAIK, safe to use on background threads ( I've not had a crash from it yet). Efficiency is about the same, as UIKit just uses CG under the hood. Key words for this are CGBitmapContextCreate, CGContextDrawImage, CGBitmapContextCreateImage, CGContextTranslateCTM, CGContextScaleCTM and CGContextRelease ( no ARC for Core Graphics). The scaling and translating is because CG has the origin in the bottom right hand corner and Y inscreases upwards.

还有第三种方法,就是使用CG作为上下文,但通过使用CALayer保存自己所有的协调痛苦,设置您的CGImage(UIImage.CGImage)作为内容,然后将图层渲染到上下文。这仍然是线程安全的,让层可以处理所有的转换。关键字是 - renderInContext:

There is also a third way, which is to use CG for the context, but save yourself all the co-ordinate pain by using a CALayer, set your CGImage ( UIImage.CGImage) as the contents and then render the layer to the context. This is still thread safe and lets the layer take care of all the transformations. Keywords for this is - renderInContext:

这篇关于垂直拼接/合成多个图像并保存为一个图像(iOS,objective-c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆