簡體   English   中英

從FilePath的NSArray獲取圖像?

[英]Fetch Images from NSArray of FilePath?

我正在嘗試獲取我存儲在以下代碼中顯示的目錄中的圖像。 我已經在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;
    }
}

嘗試這個

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

嗨,您可以這樣獲取:

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

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM