Получить все изображения из NSDocumentDirectory и сохранить в массив - PullRequest
1 голос
/ 04 октября 2011

В настоящее время я использую эти коды для сохранения своих изображений в NSDocumentDirectory.Я использую этот счетчик как соглашение об именах для них.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo 
{
  [self.popoverController dismissPopoverAnimated:YES];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDir = [paths objectAtIndex:0];
  NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", counter]];
  UIImage *image = imageView.image;
  NSData *imageData = UIImagePNGRepresentation(image);
  [imageData writeToFile:savedImagePath atomically:NO];
}

Я использую этот метод, потому что мне проще получить их все с помощью цикла.Я хочу получить все изображения из NSDocumentDirectory, чтобы я мог отобразить их в другом виде.Следующие коды показывают, как я их получаю.

-(NSMutableArray *)GetImage:(NSMutableArray *)arrayImgNames
{
  NSMutableArray *tempArray;
  for(int i=0;i<[arrayImgNames count]; i++)
  {
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths1 objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent: [arrayImgNames objectAtIndex:i]];
    [tempArray addObject:[[UIImage alloc] initWithContentsOfFile:filePath]];
    return tempArray;
  }
}

Однако я не хочу использовать счетчик в качестве соглашения об именовании для моих изображений.Я хочу использовать для них собственные имена, но если я это сделаю, мне придется изменить свой метод получения всех изображений.

Есть ли другой способ, которым я могу получить все изображения, кроме этого метода, который я упомянул

Ответы [ 2 ]

2 голосов
/ 04 октября 2011

Вы можете получить файлы, используя следующий подход:

NSURL *url = [[AppDelegate sharedAppDelegate] applicationDocumentsDirectory];
NSError *error = nil;
NSArray *properties = [NSArray arrayWithObjects: NSURLLocalizedNameKey, NSURLLocalizedTypeDescriptionKey, nil];

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url
                  includingPropertiesForKeys:properties options:(NSDirectoryEnumerationSkipsPackageDescendants)
                  error:&error];

В files будут сохранены пути ко всем файлам каталога документов.Следующий код поможет вам получить там имена:

NSURL *url = [files objectAtIndex:index];
NSString *localizedName = [url lastPathComponent];
0 голосов
/ 10 февраля 2015

Получить все изображения из NSDocumentDirectory и сохранить их в uiimageview viewcontroller.m

  • (void) viewDidLoad {[super viewDidLoad];

    for (int i=0; i<5; i++) {
    
    imgview=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    

    NSString * docDirectory = [sysPaths objectAtIndex: 0];

    NSString *str=[NSString stringWithFormat:@"img%d.jpg",i];
    
    NSLog(@"abc %@",str);
    

    NSString * imagePath = [docDirectory stringByAppendingPathComponent: str];

    imgview.image =UIImage imageWithData: [NSData dataWithContentsOfFile: imagePath]];
    [self.view addSubview: imgview];[imgview setUserInteractionEnabled: YES];

    x=x+width+margin;
    if (x>=totalwidth) {
    
        y=y+height+margin;
        x=margin;
    
    }    
    

    }}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...