IOS保存图片到沙盒并显示图片

将应用需要的一些图片存入沙盒是个不错的选择,而且应用程序可以直接通过路径去方法沙盒中的图片,在这里我们将图片存入沙盒中的Documents目录下。

  View Code

  代码如下

  #pragma mark 保存图片到document

  - (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName

  {

  NSData* imageData = UIImagePNGRepresentation(tempImage);

  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

  NSString* documentsDirectory = [paths objectAtIndex:0];

  // Now we get the full path to the file

  NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

  // and then we write it out

  [imageData writeToFile:fullPathToFile atomically:NO];

  }

  从Documents目录下获取图片

  要从Documents下面获取图片,我们首先需要获取Documents目录的路径。

  View Code

  代码如下

  #pragma mark 从文档目录下获取Documents路径

  - (NSString *)documentFolderPath

  {

  return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

  }

 

如果要显示图片需通过如下方式:

UIImage *_image = [UIImage imageWithContentsOfFile:fullPathToFile];

猜你喜欢

转载自vbtboy.iteye.com/blog/1991447
今日推荐