NSDocumentDirectory加载图像

我能够将我的数组文件保存在我的NSDocumentDirectory 。 这是我用的:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]]; ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation]; UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; //NSData *imageData = UIImagePNGRepresentation(image); [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES]; 

我的问题是如何加载这个格式的@"Images%d.png"到这个代码:

。H

 @property (strong, nonatomic) NSMutableArray *images; 

.M

  self.images = ?????? 

这是从包中读取图像名称的代码。 请修改束path到文档目录path。 代码片段将为您提供sorting图像名称的数组,也可以省去使用for-loop来读取名称。

 NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil]; NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"]; NSArray *unsortedImages = [dirContents filteredArrayUsingPredicate:filter]; NSArray *sortedImages = [unsortedImages sortedArrayUsingComparator: ^NSComparisonResult (id obj1, id obj2){ return [(NSString*)obj1 compare:(NSString*)obj2 options:NSNumericSearch]; }]; 

友好的build议:避免将任何可下载的数据/图像存储在NSDocumentDirectory目录中,因为应用程序可能会被拒绝。 改用NSCachesDirectory 。 请阅读苹果开发者平台上的这篇文章和相关文档。

干杯!!
阿马尔。

将图像保存到NSDocumentDirectory

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];//m_nImageIndex NSString *imagename = [NSString stringWithFormat:@"savedImage%d.png",m_nImageIndex]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imagename]; // Get PNG data from following method NSData *myData = UIImagePNGRepresentation(image); // It is better to get JPEG data because jpeg data will store the location and other related information of image. [myData writeToFile:savedImagePath atomically:NO]; //[UIImageJPEGRepresentation(image ,1) writeToFile:savedImagePath atomically:NO]; 

用于检索图像

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"savedImage%d.png",indexPath.row]]; UIImage *img = [UIImage imageWithContentsOfFile:getImagePath]; cell.backgroundView = [[UIImageView alloc] initWithImage:img]; 

您正在保存一个PNG扩展名为JPG文件。 我相信这应该工作:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.jpg", i]]; ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation]; UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]]; [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES]; // to load the image later: UIImage *imageFromDisk = [UIImage imageWithContentsOfFile:savedImagePath];