从FilePath的NSArray中获取图像?

时间:2014-02-27 05:45:21

标签: ios uiimage nsarray uicollectionviewcell

我正在尝试获取我存储在目录中的图像,我在下面的代码中显示了该图像。我在StachOverFlow和Chats中尝试过很多但是无法完成任务。实际上我想从filePath数组中生成存储图像路径的图像数组。我将在UICollectionView中显示。请检查我的代码并告诉我可以做些什么来实现所需。提前致谢

我已经生成了一系列文件路径,我只想从中获取图像并在网格视图中显示

-(void)plistRender{
    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"PhotoBucket.plist"];
    //pngDATA.....
    NSString *totalName = [NSString stringWithFormat:@"EGK_%@ ", [NSDate date]];
    PhotodocumentsPath = [paths objectAtIndex:0];
    PhotofilePath = [PhotodocumentsPath stringByAppendingPathComponent:totalName]; //Add the file name

    NSData *pngData = UIImagePNGRepresentation(printingImage);

    //Write image to the file directory .............
    [pngData writeToFile:[self documentsPathForFileName:PhotofilePath] atomically:YES];
    [photos_URL addObject:PhotofilePath];
    [photos addObject:totalName];
    [grid_copy addObject:[NSNumber numberWithInteger:count]];
    [grids addObject:whichProduct];
    [Totalamount addObject:[NSNumber numberWithInteger:amt]];
    NSDictionary *plistDictionary = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: photos,grids,grid_copy,Totalamount,nil] forKeys:[NSArray arrayWithObjects: @"Photo_URL",@"Product",@"Copy",@"Amount", nil]];

    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData)
    {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
    }
    else
    {
        NSLog(@"Error in saveData: %@", error);
    }
    NSString *string = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding];

    NSLog(@" plist Data %@", string);

}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"PhotoBucket"]){
        RecipeCollectionViewController *photoBucket = [segue destinationViewController];
        NSLog(@"Prepare Segue%@",photoBucket.photoCollection);
        NSLog(@"Number of photos %@",photos_URL);
        NSMutableArray *imgQueue = [[NSMutableArray alloc] initWithCapacity:photos_URL.count];
        for (NSString* path in photos_URL) {
            [imgQueue addObject:[UIImage imageWithContentsOfFile:path]];
        }
        photoBucket.photoCollection = imgQueue;
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个

for(int i=0;i<[filePathsArray count];i++)
  {

       NSString *strFilePath = [filePathsArray objectAtIndex:i];
      if ([[strFilePath pathExtension] isEqualToString:@"jpg"] || [[strFilePath pathExtension] isEqualToString:@"png"] || [[strFilePath pathExtension] isEqualToString:@"PNG"]) 
      {
         NSString *imagePath = [[stringPath stringByAppendingFormat:@"/"] stringByAppendingFormat:strFilePath];
         NSData *data = [NSData dataWithContentsOfFile:imagePath];
        if(data)
        {
          UIImage *image = [UIImage imageWithData:data];
       }
   }
}

答案 1 :(得分:0)

嗨,你可以这样取:

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

NSString *documentsDirectoryStr1 = [pathPlist1 objectAtIndex:0];

NSString *plistLocation1 =
[documentsDirectoryStr1 stringByAppendingPathComponent:@"ImageStorage"];

NSFileManager *filemgr;
NSArray *filelist;
int i;

filemgr =[NSFileManager defaultManager];
filelist = [filemgr contentsOfDirectoryAtPath:plistLocation1 error:NULL];

NSLog(@"filelist =%lu",[filelist count]);

cacheImagesArray=[[NSMutableArray alloc] init];
cacheImagesDataArray=[[NSMutableArray alloc] init];

for (i = 0; i < [filelist count]; i++){
    NSLog(@"%@", [filelist objectAtIndex: i]);
    NSString *imageName=[NSString stringWithFormat:@"%@",[filelist objectAtIndex: i]];
    NSString *path=[NSString stringWithFormat:@"%@/%@",plistLocation1, [filelist objectAtIndex: i]];
    NSLog(@"Path is =%@",path);
    NSData* data = [NSData dataWithContentsOfFile:path];
    [cacheImagesDataArray addObject:data];
    [cacheImagesArray addObject:imageName];
}

感谢