0

我正在尝试以这种方式保存我的设备的屏幕截图以供以后使用:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
     UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();                       

//NSString *tmpPngFile = [NSTemporaryDirectory() stringByAppendingPathComponent:@"background55667.png"];
  NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/background55667.png";
 [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

...但我似乎无法打开它并在 UIImageView 中显示它。像这样的东西:

NSString *documentsDirectory = [NSHomeDirectory()  stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);

UIImage *image = [UIImage imageNamed:....];
backGroundPhoto.image = image;

我该怎么做?

4

1 回答 1

2

[UIImage imageNamed:]用于加载已包含在应用程序包中的图像。

假设您的路径是正确的,您可以通过

UIImage *image = [UIImage imageWithContentsOfFile: pngPath];
于 2013-01-21T16:57:55.443 回答