//截屏 static int i=0; -(IBAction)screenShot:(id)sender{ UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0); [[self.window layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = viewImage.CGImage; CGRect rect =CGRectMake(166, 211, 426, 320);//這裡可以設置想要截圖的區域 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= [NSString stringWithFormat:@"screenShow_%d.png",i]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; NSLog(@"%@", savedImagePath); [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目錄 CGImageRelease(imageRefRect); i++; }