ios截屏代码[转]
这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员很有帮助
http://www.2cto.com/kf/201310/250228.html
我将其改为以下代码:
1 #pragma mark -=====自定义截屏位置大小的逻辑代码=====- 2 static int ScreenshotIndex=0; //这里的逻辑直接采用上面博主的逻辑了 3 -(void)ScreenShot{ 4 //这里因为我需要全屏接图所以直接改了,宏定义iPadWithd为1024,iPadHeight为768, 5 // UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); //设置截屏大小 6 UIGraphicsBeginImageContextWithOptions(CGSizeMake(iPadWidth, iPadHeight), YES, 0); //设置截屏大小 7 [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()]; 8 UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 9 UIGraphicsEndImageContext(); 10 CGImageRef imageRef = viewImage.CGImage; 11 // CGRect rect = CGRectMake(166, 211, 426, 320);//这里可以设置想要截图的区域 12 CGRect rect = CGRectMake(0, 0, iPadWidth, iPadHeight);//这里可以设置想要截图的区域 13 CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect); 14 UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; 15 UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库 16 NSData *imageViewData = UIImagePNGRepresentation(sendImage); 17 18 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 19 NSString *documentsDirectory = [paths objectAtIndex:0]; 20 NSString *pictureName= [NSString stringWithFormat:@"screenShow_%d.png",ScreenshotIndex]; 21 NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; 22 NSLog(@"截屏路径打印: %@", savedImagePath); 23 //这里我将路径设置为一个全局String,这里做的不好,我自己是为了用而已,希望大家别这么写 24 [self SetPickPath:savedImagePath]; 25 26 [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录 27 CGImageRelease(imageRefRect); 28 ScreenshotIndex++; 29 } 30 //设置路径 31 - (void)SetPickPath:(NSString *)PickImage { 32 _ScreenshotsPickPath = PickImage; 33 } 34 //获取路径<这里我就直接用于邮件推送的代码中去了,能达到效果,但肯定有更好的写法> 35 - (NSString *)GetPickPath { 36 return _ScreenshotsPickPath; 37 }
重要备注: 上面代码只适用于截取普通视图如:UIScrollView、UIView、UIImageView等等,不适用于OpenGL ES、相机内容等,若需要实现对三维模型或者拍照一类截屏功能,还需要另外寻找截取OpenGL渲染区部分的截屏代码或者截取相机内容的截屏代码。
吾屋无污,吴捂屋污,诬吾污屋,吾晤吴污,吾无物污,吴侮污吾,吾忤吴屋,悟吴诬吾,污屋吴污,吴侮吾污,吾勿武侮,吾唔吴误,诬侮污吾,吾屋悟忤.