您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關iOS如何實現截屏功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
1.普通界面
/** *截圖功能 */ -(void)screenShot{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); //設置截屏大小 [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = viewImage.CGImage; CGRect rect = CGRectMake(0, 0, 641, SCREEN_HEIGHT + 300);//這里可以設置想要截圖的區域 CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect); UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; //以下為圖片保存代碼 UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存圖片到照片庫 NSData *imageViewData = UIImagePNGRepresentation(sendImage); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pictureName= @"screenShow.png"; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目錄 CGImageRelease(imageRefRect); //從手機本地加載圖片 UIImage *bgImage2 = [[UIImage alloc]initWithContentsOfFile:savedImagePath]; }
2.UIScrollView截屏(一屏無法顯示完整)
/** *截圖 */ - (void)screenShot{ UIImage* image = nil; UIGraphicsBeginImageContext(m_scrollView.contentSize); { CGPoint savedContentOffset = m_scrollView.contentOffset; CGRect savedFrame = m_scrollView.frame; m_scrollView.contentOffset = CGPointZero; m_scrollView.frame = CGRectMake(0, 0, m_scrollView.contentSize.width, m_scrollView.contentSize.height); [m_scrollView.layer renderInContext: UIGraphicsGetCurrentContext()]; image = UIGraphicsGetImageFromCurrentImageContext(); m_scrollView.contentOffset = savedContentOffset; m_scrollView.frame = savedFrame; } UIGraphicsEndImageContext(); if (image != nil) { NSLog(@"截圖成功!"); } }
感謝各位的閱讀!關于“iOS如何實現截屏功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。