-(void) viewdidload
{
[Super Viewdidload];
1. Return watermark Image
UIImage *newimage = [UIImage waterimagewithbg:@ "Scene" logo:@ "logo"];
2. Show pictures
Self.iconView.image = NewImage;
}
-(void) Testwater
{
UIImage *bgimage = [UIImage imagenamed:@ "scene"];
Ober: Based on bitmap (bitmap), all things need to be drawn to a new picture up
1. Create a bitmap-based context (open a bitmap-based context)
Size: The dimensions of the new picture
Opaque:yes: Opaque, no: transparent
This line of code is followed by a common new bitmap, the new UIImage object.
Uigraphicsbeginimagecontextwithoptions (Bgimage.size, NO, 0.0);
2. Painting background
[Bgimage drawinrect:cgrectmake (0, 0, BgImage.size.width, bgImage.size.height)];
3. Draw the watermark in the lower right corner
UIImage *waterimage = [UIImage imagenamed:@ "logo"];
CGFloat scale = 0.2;
CGFloat margin = 5;
CGFloat WATERW = waterImage.size.width * scale;
CGFloat waterh = waterImage.size.height * scale;
CGFloat Waterx = Bgimage.size.width-waterw-margin;
CGFloat watery = bgimage.size.height-waterh-margin;
[Waterimage Drawinrect:cgrectmake (Waterx, watery, waterw, Waterh)];
4. Get the finished UIImage object from the context
UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
5. End Context
Uigraphicsendimagecontext ();
6. Show to Uiimageview
Self.iconView.image = NewImage;
7. Compressing the image object to binary data in PNG format
NSData *data = uiimagepngrepresentation (newimage);
UIImageJPEGRepresentation (< #UIImage *image#>, < #CGFloat compressionquality#>)
8. Writing files
NSString *path = [[Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "New.png"];
[Data Writetofile:path Atomically:yes];
}
Add a category to image
/**
* Water Stamp
* Return a picture with a watermark
* @param BG Background image
* @param watermark image in the bottom right corner of logo
*/
+ (Instancetype) WATERIMAGEWITHBG: (NSString *) BG Logo: (NSString *) logo
{
UIImage *bgimage = [UIImage IMAGENAMED:BG];
1. Create a bitmap-based context (open a bitmap-based context)
Uigraphicsbeginimagecontextwithoptions (Bgimage.size, NO, 0.0);
2. Painting background
[Bgimage drawinrect:cgrectmake (0, 0, BgImage.size.width, bgImage.size.height)];
3. Draw the watermark in the lower right corner
UIImage *waterimage = [UIImage Imagenamed:logo];
CGFloat scale = 0.2;
CGFloat margin = 5;
CGFloat WATERW = waterImage.size.width * scale;
CGFloat waterh = waterImage.size.height * scale;
CGFloat Waterx = Bgimage.size.width-waterw-margin;
CGFloat watery = bgimage.size.height-waterh-margin;
[Waterimage Drawinrect:cgrectmake (Waterx, watery, waterw, Waterh)];
4. Get the finished UIImage object from the context
UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();
5. End Context
Uigraphicsendimagecontext ();
return newimage;
}
Image watermark (personal signature on Weibo image)