iOS screenshot and modify screenshots then share the feature implementation

Source: Internet
Author: User

I. The effect of implementation similar to Weibo sharing

Not only to share the time also to make changes in the picture, add their own QR code

Two. How to Implement

Apple provides a new type of notification after iOS7:uiapplicationuserdidtakescreenshotnotification,

This notification informs the object that the notification has been registered that a screenshot event has occurred, and then we can implement our own logic in this event

1. Registration Notice

-(void) viewdidload {    [super Viewdidload];     // screenshot operation notification for registered users     [[Nsnotificationcenter defaultcenter] addobserver:self                                             selector: @selector (userdidtakescreenshot:)                                                  Object : nil];}

2. Receive notifications (get and modify images, and show, display UI, can modify yourself)

//screenshot Response- (void) Userdidtakescreenshot: (Nsnotification *) notification{NSLog (@"screenshot Detected"); //Artificial screenshot, simulate the user screenshot behavior, get the film_testimg =[self imagewithscreenshot]; //    //Add displayUiimageview *imgvphoto =[[Uiimageview alloc]initwithimage:_testimg]; Imgvphoto.frame= CGRectMake (0, win_height/2, win_width/2, win_height/2); Imgvphoto.backgroundcolor=[Uicolor Orangecolor]; Imgvphoto.userinteractionenabled=YES; //Add a borderCalayer * Layer =[Imgvphoto layer]; Layer.bordercolor=[[Uicolor Whitecolor] cgcolor]; Layer.borderwidth=5.0f; //add four edge shadowsImgvPhoto.layer.shadowColor =[Uicolor Blackcolor].    Cgcolor; ImgvPhoto.layer.shadowOffset= Cgsizemake (0,0); ImgvPhoto.layer.shadowOpacity=0.5; ImgvPhoto.layer.shadowRadius=10.0; //Add two edge shadowsImgvPhoto.layer.shadowColor =[Uicolor Blackcolor].    Cgcolor; ImgvPhoto.layer.shadowOffset= Cgsizemake (4,4); ImgvPhoto.layer.shadowOpacity=0.5; ImgvPhoto.layer.shadowRadius=2.0;        [Self.view Addsubview:imgvphoto]; //Add gesturesUITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (Tapimgview:)]; [Imgvphoto Addgesturerecognizer:tap];}

3. and modify the picture

/** * Capture the current screen and modify * * @return NSData **/-(UIImage *) imagewithscreenshot{cgsize imageSize=Cgsizezero; Uiinterfaceorientation Orientation=[UIApplication sharedapplication].statusbarorientation; if(uiinterfaceorientationisportrait (orientation)) ImageSize=[UIScreen mainscreen].bounds.size; ElseimageSize=Cgsizemake ([UIScreen mainscreen].bounds.size.height, [UIScreen mainscreen].bounds.size.width]; Uigraphicsbeginimagecontextwithoptions (ImageSize, NO,0); Cgcontextref Context=Uigraphicsgetcurrentcontext ();  for(UIWindow *windowinch[[UIApplication sharedapplication] windows])        {cgcontextsavegstate (context);        CGCONTEXTTRANSLATECTM (context, window.center.x, window.center.y);        CGCONTEXTCONCATCTM (context, window.transform); CGCONTEXTTRANSLATECTM (Context,-window.bounds.size.width * window.layer.anchorpoint.x,-window.bounds.size.height *window.layer.anchorpoint.y); if(Orientation = =uiinterfaceorientationlandscapeleft)            {CGCONTEXTROTATECTM (context, m_pi_2); CGCONTEXTTRANSLATECTM (Context,0, -imagesize.width); }Else if(Orientation = =uiinterfaceorientationlandscaperight) {CGCONTEXTROTATECTM (context,-m_pi_2); CGCONTEXTTRANSLATECTM (Context,-imagesize.height,0); } Else if(Orientation = =Uiinterfaceorientationportraitupsidedown)            {CGCONTEXTROTATECTM (context, M_PI); CGCONTEXTTRANSLATECTM (Context,-imagesize.width,-imagesize.height); }        if([Window respondstoselector: @selector (drawviewhierarchyinrect:afterscreenupdates:)]) {[Window Draw        ViewHierarchyInRect:window.bounds Afterscreenupdates:yes]; }        Else{[Window.layer renderincontext:context];    } cgcontextrestoregstate (context); } UIImage*image =Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext (); //Modify a pictureNSData *imagedata =uiimagepngrepresentation (image); UIImage*lastimage =[UIImage Imagewithdata:imagedata]; UIImage*img = [UIImage imagenamed:@"Ico_nursery.png"]; Cgimageref Imgref=img.    Cgimage; CGFloat W=cgimagegetwidth (IMGREF); CGFloat h=cgimagegetheight (IMGREF); //base map with 1.png diagram sizeUIImage *IMG1 =Lastimage; Cgimageref IMGREF1=IMG1.    Cgimage; CGFloat W1=cgimagegetwidth (IMGREF1); CGFloat H1=cgimagegetheight (IMGREF1); //Create a context for the canvas in 1.png diagram sizeUigraphicsbeginimagecontext (Cgsizemake (W1, H1 + -)); [Img1 Drawinrect:cgrectmake (0,0, W1, H1)];//draw 1.png into the context first[img Drawinrect:cgrectmake (Ten, H1 +Ten, the, the)];//And put the little picture in context.UIImage *resultimg = Uigraphicsgetimagefromcurrentimagecontext ();//get the final picture from the current contextUigraphicsendimagecontext ();//Close Context        returnresultimg;}

4. Share your share according to the added events can also be encapsulated

//Click on the picture to change ImageView location, print the picture information share oneself also can encapsulate- (void) Tapimgview: (UITapGestureRecognizer *) Tap {NSLog (@"Click on the picture ..."); // [Myapiclient mobevent:@"WeChat"];//[Helper shareimagename:_testimg type:ssdkplatformsubtypewechatsession];//Friends[Helper shareimagename:_testimg Type:ssdkplatformsubtypewechattimeline];//Circle of Friends//QQ//[myapiclient mobevent:@ "QQ"];//[Helper shareimagename:_testimg type:ssdkplatformtypeqq];//QQ    }

5. Remove Notifications

-(void) dealloc{    [[Nsnotificationcenter defaultcenter]removeobserver:self];}

That's it. Show me the test.

Their own

Edit Share Map

OK, end, need to add, welcome to leave a message to discuss!

iOS screenshot and modify then share the feature implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.