ios - 从 FilePath 的 NSArray 获取图像?

标签 ios uiimage nsarray uicollectionviewcell

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

我已经生成了文件路径数组,我只想从中获取图像并在 GridView 中显示

-(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;
    }
}

最佳答案

试试这个

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];
       }
   }
}

关于ios - 从 FilePath 的 NSArray 获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22060029/

相关文章:

ios - 如何初始化 NSString 的 NSArray?

ios - SIGSEGV 错误发生在实际设备上但在 ios 模拟器上没有

ios - 体系结构 x86_64 : for Firebase in Xcode 8. 3.2 的 undefined symbol

iphone - 帮助我了解此代码 : 的泄漏

arrays - Swift - 无法为索引类型为 NSNumber 的值类型 [String] 下标?

ios - 错误:无法推断出通用参数“元素”

ios - 从 plutil 获取信息

ios - 如何在曲面上添加阴影?

ios - 在 iOS 中考虑 alpha 部分在另一个图像上添加图像

iphone - 当数组的计数已知时,按顺序用数字初始化 NSArray