博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
混合两张图
阅读量:6847 次
发布时间:2019-06-26

本文共 4173 字,大约阅读时间需要 13 分钟。

在iOS中如何将两张图混合成一张图呢?比如有一张是透明图,另外一张是不透明的,混合为一张图,并且保存起来。有关保存可见。

上图所示,可将红色手绘的色彩图片和原来的图层混合。

 

代码如下:

-(void)generateImageToPhotoAlbum{ 

    NSLog(@"save image to photo album."); 
    
    UIGraphicsBeginImageContext(CGSizeMake(1024, 768)); 
    [drawImage.image drawInRect:self.frame]; 
    
    UIImage *foregroundImage=[UIImage imageWithCGImage:(CGImageRef)foregroundLayer.contents]; 
    [foregroundImage drawInRect:self.frame blendMode:kCGBlendModeNormal alpha:1.0]; 
    
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    
    UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil); 
}

本文参考了:

错误代码:

I'm trying to blend a background with a foreground image, where the foreground image is a transparent image with lines on it.

I am trying to do it this way.

1 UIGraphicsBeginImageContext(CGSizeMake(320,480));  2 CGContextRef context =UIGraphicsGetCurrentContext();    4 // create rect that fills screen  5 CGRect bounds =CGRectMake(0,0,320,480);  7 // This is my bkgnd image  8 CGContextDrawImage(context, bounds,[UIImage imageNamed:@"bkgnd.jpg"].CGImage);  9 CGContextSetBlendMode(context, kCGBlendModeSourceIn); //CGContextSetAlpha(); 11 // This is my image to blend in 12 CGContextDrawImage(context, bounds,[UIImage imageNamed:@"over.png"].CGImage); 14 UIImage*outputImage =UIGraphicsGetImageFromCurrentImageContext(); 16 UIImageWriteToSavedPhotosAlbum(outputImage,self,nil,nil); 17 // clean up drawing environment 18 UIGraphicsEndImageContext();

but does not seem to work.

Any suggestions will be appreciated.

 

方法1:上层图片设置绘画属性    【 drawInRect:  blendMode:  alpha: 】    

This is what I've done in my app, similar to Tyler's - but without the UIImageView:

1 UIImage*bottomImage =[UIImage imageNamed:@"bottom.png"];  2 UIImage*image =[UIImage imageNamed:@"top.png"];  4 CGSize newSize =CGSizeMake(width, height);  5 UIGraphicsBeginImageContext( newSize );  7 // Use existing opacity as is  8 [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  9 // Apply supplied opacity 10 [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8]; 12 UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext(); 14 UIGraphicsEndImageContext();
 
方法2:两张图片放在同一个UIImageView中,然后用
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()]  渲染。
1 UIImage* bottomImage = [UIImage imageNamed:@"bottom.png"];   2 UIImage* topImage    = [UIImageNamed:@"top.png"];  3 UIImageView* imageView = [[UIImageView alloc] initWithImage:bottomImage];  4 UIImageView* subView   = [[UIImageView alloc] initWithImage:topImage];  5 subView.alpha = 0.5;  // Customize the opacity of the top image.  6 [imageView addSubview:subView];  7 UIGraphicsBeginImageContext(imageView.frame.size);  8 [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];  9 UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext(); 10 UIGraphicsEndImageContext(); 11 [subView release]; 12 [imageView release]; 13 14 [self doWhateverIWantWith: blendedImage];
If there are alpha values within the top image's png file, just leave out the "subView.alpha = 0.5;" line, and it will draw the top image, including it's custom alpha values, on top of the bottom image. 
 
Blending with alpha
 
1 UIGraphicsBeginImageContext(area.size);  2 CGContextRef context = UIGraphicsGetCurrentContext();  3 CGContextRetain(context);  4  5 // mirroring context  6 CGContextTranslateCTM(context, 0.0, area.size.height);  7 CGContextScaleCTM(context, 1.0, -1.0);  8  9 for (...) {
10 CGContextBeginTransparencyLayer(context, nil); 11 CGContextSetAlpha( context, alpha ); 12 CGContextDrawImage(context, area, tempimg.CGImage); 13 CGContextEndTransparencyLayer(context); 14 } 15 16 // get created image 17 UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 18 CGContextRelease(context); 19 UIGraphicsEndImageContext();
 

Suggestion: You can use UIImage's drawInRect: or drawAtPoint: instead of CGContextDrawImage (they draw to the current context). Does using them give you any difference in output?

 

It may also be helpful to make sure the UIImage* values you are getting back from imageNamed:are valid.

 

转载于:https://www.cnblogs.com/pengyingh/articles/2344808.html

你可能感兴趣的文章
JB的Python之旅-爬虫篇-图形验证码(2)-- 收费OCR了解下
查看>>
scroll-view组件bindscroll实例应用:自定义滚动条
查看>>
使用Flutter CustomPainter绘制8段数码管
查看>>
干货:ActiveMQ高并发处理方案
查看>>
百度架构师是怎样搭建MySQL分布式集群?
查看>>
jvm优化必知系列——监控工具
查看>>
javascript的this原理
查看>>
Hexo已经看腻了,来试试VuePress搭建个人博客
查看>>
聊聊V8引擎的垃圾回收
查看>>
你可能不知道的JS
查看>>
你知道前端对图片的处理方式吗?
查看>>
UML
查看>>
ES6-学习之路-05
查看>>
Python-shutil模块
查看>>
Python 黑帽编程 2.2 数值类型
查看>>
Android持续集成:Jenkins+Gradle+360加固+多渠道打包
查看>>
基于CoreGraphics的3D渲染方案
查看>>
基于VUE的服务器端渲染(SSR)
查看>>
『中级篇』Docker compose 部署一个复杂的应用(41)
查看>>
『中级篇』容器的端口映射(28)
查看>>