iOS截屏並修改截圖然後分享的功能實現

來源:互聯網
上載者:User

標籤:reg   ima   點擊   frame   tco   微博   tran   graphics   white   

一. 實現的效果類似微博的分享

不僅分享的時候還進行圖片的修改,增加自己的二維碼

二.實現方式

蘋果在ios7之後提供了一個新的通知類型:UIApplicationUserDidTakeScreenshotNotification,

這個通知會告知註冊了此通知的對象已經發生了截屏事件,然後我們就可以在這個事件中實現自己的邏輯

1.註冊通知

- (void)viewDidLoad {    [super viewDidLoad];    //註冊使用者的截屏操作通知    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(userDidTakeScreenshot:)                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];}

2.接收通知 (擷取並修改的圖片,並展示,展示UI,可以自己修改)

//截屏響應- (void)userDidTakeScreenshot:(NSNotification *)notification{    NSLog(@"檢測到截屏");        //人為截屏, 類比使用者截屏行為, 擷取所片    _testImg = [self imageWithScreenshot];        //    //添加顯示    UIImageView *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;    //添加邊框    CALayer * layer = [imgvPhoto layer];    layer.borderColor = [[UIColor whiteColor] CGColor];    layer.borderWidth = 5.0f;    //添加四個邊陰影    imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;    imgvPhoto.layer.shadowOffset = CGSizeMake(0, 0);    imgvPhoto.layer.shadowOpacity = 0.5;    imgvPhoto.layer.shadowRadius = 10.0;    //添加兩個邊陰影    imgvPhoto.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];        // 添加手勢    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgView:)];    [imgvPhoto addGestureRecognizer:tap];}

3. 並修改圖片

/** *  截取當前螢幕 並修改 * *  @return NSData * */- (UIImage *)imageWithScreenshot{    CGSize imageSize = CGSizeZero;    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;    if (UIInterfaceOrientationIsPortrait(orientation))        imageSize = [UIScreen mainScreen].bounds.size;    else        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);    CGContextRef context = UIGraphicsGetCurrentContext();    for (UIWindow *window in [[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 drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];        }        else        {            [window.layer renderInContext:context];        }        CGContextRestoreGState(context);    }        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        // 修改圖片    NSData *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);        //以1.png的圖大小為底圖    UIImage *img1 = LastImage;    CGImageRef imgRef1 = img1.CGImage;    CGFloat w1 = CGImageGetWidth(imgRef1);    CGFloat h1 = CGImageGetHeight(imgRef1);        //以1.png的圖大小為畫布建立上下文    UIGraphicsBeginImageContext(CGSizeMake(w1, h1 + 100));    [img1 drawInRect:CGRectMake(0, 0, w1, h1)];//先把1.png 畫到上下文中    [img drawInRect:CGRectMake(10, h1 + 10, 80, 80)];//再把小圖放在上下文中    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//從當前上下文中獲得最終圖片    UIGraphicsEndImageContext();//關閉上下文        return  resultImg;}

4.根據添加的事件進行分享 分享自己也可封裝

// 點擊圖片改變imageView位置,列印圖片資訊  分享自己也可封裝- (void)tapImgView: (UITapGestureRecognizer *)tap {        NSLog(@"點擊了圖片..."); //     [MyAPIClient mobEvent:@"wechat"];//  [Helper shareImageName:_testImg type:SSDKPlatformSubTypeWechatSession];//  好友    [Helper shareImageName:_testImg type:SSDKPlatformSubTypeWechatTimeline];// 朋友圈// QQ//    [MyAPIClient mobEvent:@"QQ"];//    [Helper shareImageName:_testImg type:SSDKPlatformTypeQQ];// QQ    }

5. 移除通知

- (void)dealloc{    [[NSNotificationCenter defaultCenter]removeObserver:self];}

這樣就可以了.展示一下測試

自身

修改分享圖

 

ok,結束,需要補充的,歡迎大家留言討論!

 

iOS截屏並修改然後分享的功能實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.