UIView *view = [[[[[UIApplication sharedApplication] windows] objectAtIndex:1] subviews] lastObject];//獲得某個window的某個subView NSInteger index = 0;//用來給保存的png命名 for (UIView *subView in [view subviews]) {//遍歷這個view的subViews if ([subView isKindOfClass:NSClassFromString(@"UIImageView")] || [subView isKindOfClass:NSClassFromString(@"UIThreePartButton")]) {//找到自己需要的subView 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!"); } else {
附:截屏代碼
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
在91助手上常見iPhone屏幕截圖的功能,,于是就開始想辦法,,看能不能手動把屏幕截圖做出來。下面這個函數(shù)的代碼,,就是根據(jù)UIView上的 顯示內(nèi)容生成一張屏幕截圖,。值得注意的是,,其中引用了Quartz2D中的函數(shù),所以要使用的話要在文件頭部增加一個引用,。 #import QuartzCore/QuartzCore.h -(UIImage *)getImageFromView:(UIView *)orgView{ UIGraphicsBeginImageContext(orgView.bounds.size); [orgView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
|