加载已保存的UIImage

时间:2013-01-21 15:17:51

标签: xcode uiimage

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

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;

我该怎么做?

1 个答案:

答案 0 :(得分:2)

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

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

加载它
UIImage *image = [UIImage imageWithContentsOfFile: pngPath];