This demand may sometimes encounter, such as their own photos plus copyright, watermark, etc.
Online method, there are many feelings are not all right, or demand is not special, here I summed up 3 scenarios under the requirements:
1. Local Picture Synthesis text
2, the local picture synthesis picture
3, the network picture first download and then synthesize the picture
:
The size of the composition here, I have been casually written, not intentionally calculated, we can customize according to the actual needs.
Code section:
/** Image Synthetic Text @param img < #img description#> @param logotext < #logoText description#> @return < #return value Description#>*/-(UIImage *) Imageaddtext: (UIImage *) img Text: (NSString *) logotext{NSString* Mark =Logotext; intW =Img.size.width; inth =Img.size.height; Uigraphicsbeginimagecontext (img.size); [img Drawinrect:cgrectmake (0,0, W, h)]; Nsdictionary*attr = @{nsfontattributename: [Uifont boldsystemfontofsize: -], nsforegroundcolorattributename: [Uicolor Redcolor]}; //Location Display[Mark Drawinrect:cgrectmake (Ten, -, w*0.8, h*0.3) withattributes:attr]; UIImage*aimg =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnaimg; }
/** Local photo synthesis @param useimage < #useImage description#> @param maskimage < #maskImage description#> @return < ; #return value description#>*/-(UIImage *) Imageaddlocalimage: (UIImage *) useimage addmsakimage: (UIImage *) maskimage{uigraphicsbeginimagecontextwithoptions (useimage.size, NO,0.0); [Useimage Drawinrect:cgrectmake (0,0, UseImage.size.width, UseImage.size.height)]; //four parameters for the location of the watermark picture[Maskimage Drawinrect:cgrectmake (0,0, UseImage.size.width, useimage.size.height/2)]; //If you want multiple locations to appear, continue drawinrect.//[Maskimage drawinrect:cgrectmake (0, USEIMAGE.SIZE.HEIGHT/2, UseImage.size.width, USEIMAGE.SIZE.HEIGHT/2)];UIImage *resultingimage =Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); returnresultingimage;}
/** Download Web image synthesis @param imgurl < #imgUrl description#> @param imgUrl2 < #imgUrl2 description#> @param Imgview < #imgView description#>*/- (void) Imageaddurlimage: (NSString *) Imgurl image2: (NSString *) imgUrl2 Showinimageview: (Uiimageview *) imgview{//1. Initialization of queue groups, global concurrent queuesdispatch_group_t Group =dispatch_group_create (); dispatch_queue_t Queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2. The external local variables cannot be modified inside the block, which must be prefixed __block__block UIImage *image1 =Nil; //Note that there is more than one group (queue) for the async execution method hereDispatch_group_async (group, queue, ^{Nsurl*URL1 =[Nsurl Urlwithstring:imgurl]; NSData*data1 =[NSData DATAWITHCONTENTSOFURL:URL1]; Image1=[UIImage imagewithdata:data1]; }); //3. Download Picture 2__block UIImage *image2 =Nil; Dispatch_group_async (group, queue,^{Nsurl*URL2 =[Nsurl Urlwithstring:imgurl2]; NSData*DATA2 =[NSData Datawithcontentsofurl:url2]; Image2=[UIImage Imagewithdata:data2]; }); __block UIImage*Fullimage; //4. Merge pictures (ensure that all tasks within the group are executed, and then execute the block inside the Notify function)Dispatch_group_notify (group, queue, ^{uigraphicsbeginimagecontextwithoptions (image1.size, NO,0.0); [Image1 Drawinrect:cgrectmake (0,0, Image1.size.width, Image1.size.height)]; [Image2 Drawinrect:cgrectmake (0,0, Image1.size.width, image1.size.height/2)]; Fullimage=Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); Dispatch_async (Dispatch_get_main_queue (),^{imgview.image=Fullimage; }); });}
Note: The composition above the position, are I casually write, the actual situation, we can define their own requirements, or the location of the parameter can also be, the landlord is because of laziness ...
IOS image watermark, picture compositing text or picture implementation