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 に答える