前言
前段时间,由于公司接了一个书法的项目,里面迭代的内容涉及到了这个整图保存相册的功能.但是自己之前也没有真正的了解过这个具体是什么实现的,于是自己网上就搜索了很多关于这个方面的资料看看,才知道,原来这是画布就可以实现的了.好了,闲言少叙
具体实现步骤
首先很简单啊,就是要把你保存的图片或者字体存放在一个NSArray中,当然这个数组可能会是两种类型,这个定义模型的时候需要确定好其次就是开始或整张图片的背景,这背景可以是默认的白底,也可以是你想要的任何一张图片,只要的画之前把这个定义好就行接下来的画就是将你要的图片一张一张画到这个背景图片里面去了,这一步很重要.下面就直接上代码吧.
代码实现
- (
BOOL)mergedImageWithImageArray:(
NSArray *)imgArray
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(
10,
10,
10,
10);
CGFloat imageLie =
1;
CGFloat imageHang =
1;
CGFloat image_width = (CGRectGetWidth(
self.view.bounds) -
20)/
self.index_lie;
CGFloat bgImgWidth =
self.index_lie*image_width + (
self.index_lie -
1) *imageLie + edgeInsets
.left + edgeInsets
.right;
CGFloat bgImgHeight =
self.index_hang*image_width + (
self.index_hang -
1) * imageHang + edgeInsets
.top + edgeInsets
.bottom;
UIGraphicsBeginImageContext(CGSizeMake(bgImgWidth, bgImgHeight));
for (
int i =
0; i < imgArray
.count; i++)
{
id data = imgArray[i];
int lang = i % _index_hang;
int lie = i / _index_hang;
CGFloat x = (bgImgWidth - image_width -lie - (image_width + imageLie) * lie)-edgeInsets
.left;
CGFloat y = edgeInsets
.top+(image_width+imageHang)*lang;
if ([data isKindOfClass:[
UIImage class]])
{
UIImage *image = imgArray[i];
[image drawInRect:CGRectMake(x, y, image_width, image_width)];
}
else if ([data isKindOfClass:[
NSString class]])
{
NSString *text = imgArray[i];
CGFloat textSize = image_width/
2;
UIFont *font = [
UIFont systemFontOfSize:textSize];
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle
.alignment = NSTextAlignmentCenter;
paragraphStyle
.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary* attribute = @{
NSForegroundColorAttributeName:[
UIColor blackColor],
NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle,
};
CGFloat textY = y + (image_width-textSize)/
2;
[text drawInRect:CGRectMake(x, textY, image_width, image_width) withAttributes:attribute];
}
}
UIImage *newMergeImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if (newMergeImg ==
nil) {
return NO;
}
else {
UIImageWriteToSavedPhotosAlbum(newMergeImg,
self,
nil,
nil);
return YES;
}
}
最终保存效果
转载请注明原文地址: https://ju.6miu.com/read-666831.html