如何在iPhone应用程序中截取屏幕截图?

时间:2010-11-12 03:30:23

标签: iphone objective-c camera

  

可能重复:
  Access the camera with iPhone SDK

如何在iPhone应用中拍照?

我可以使用哪种API?

请提供示例代码。

1 个答案:

答案 0 :(得分:2)

UIGraphicsBeginImageContext(self.view.bounds.size);

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();   
CGContextRef context = UIGraphicsGetCurrentContext();

UIImageWriteToSavedPhotosAlbum(saveImage, self, @selector(image:didFinishSavingWithError:contextInfo:), &context);
UIGraphicsEndImageContext();

// saving image in documents folder
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSString *imageName = @"temp.png";

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];

NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:NO];