overlap one image over another

人走茶凉 提交于 2020-02-02 14:06:51

问题


I want to add image and some text on another image and create a single image. I have got to add text but not able to figure out that how to add image. Any help?


回答1:


This snippet assumes you have UIImage's named bottomImage for the base image to be drawn upon, and topImage that will be drawn ON (above) the bottomImage. xpos,ypos are floats to describe the target x,y (top left) position where topImage will be drawn, and targetSize the size in which topImage will be drawn on bottomImage.

...
    UIGraphicsBeginImageContext( bottomImage.size );//create a new image context to draw offscreen
    [bottomImage drawInRect:CGRectMake(0,0,bottomImage.size.width,bottomImage.size.height)];//draw bottom image first, at original size
    [topImage drawInRect:CGRectMake(xpos,ypos,targetSize.width,targetSize.height) blendMode:kCGBlendModeNormal alpha:1];//draw the image to be overlayed second, at wanted location and size
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();//get newly drawn image context into a UIImage
    UIGraphicsEndImageContext();//stop drawing to the context
    return newImage;//return/use the newly created image

This is not thread safe - creating a UIImage in a thread is not recommended.



来源:https://stackoverflow.com/questions/6993388/overlap-one-image-over-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!