博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS给任意UIView截屏
阅读量:7165 次
发布时间:2019-06-29

本文共 948 字,大约阅读时间需要 3 分钟。

//获得某个window的某个subViewUIView *view = [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] subviews] lastObject];NSInteger index = 0;//用来给保存的png命名for (UIView *subView in [view subviews]) {//遍历这个view的subViewsif ([subView isKindOfClass:NSClassFromString(@"MKMapView")]) {//找到自己需要的subViewNSLog(@"fund");//支持retina高分的关键if(UIGraphicsBeginImageContextWithOptions != NULL){UIGraphicsBeginImageContextWithOptions(subView.frame.size, NO, 0.0);} else {UIGraphicsBeginImageContext(subView.frame.size);}//获取图像[subView.layer renderInContext:UIGraphicsGetCurrentContext()];UIImage *image = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();//保存图像NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/%d.png",index];if ([UIImagePNGRepresentation(image) writeToFile:path atomically:YES]) {index += 1;NSLog(@"Succeeded! %@",path);}else {NSLog(@"Failed!");}}}

转载于:https://www.cnblogs.com/zzili/archive/2012/12/29/6663197.html

你可能感兴趣的文章