0

MWPhotoBrowser を使用して、アプリ ドキュメント ディレクトリに最新の画像を最初に表示する方法を教えてください。

以下のコードを使用して、アプリ ドキュメント ディレクトリに画像を保存し、画像を表示します。

- (IBAction)saveButtonPressed:(id)sender
{   
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Get documents folder
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/abc"];

//Create folder
if (![fileMgr fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

//Get the current date and time and set as image name
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd_HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *gmtTime = [dateFormatter stringFromDate:now];
NSLog(@"The Current Time is :%@", gmtTime);

NSData *imageData = UIImageJPEGRepresentation(self.imageTaken, 0.5);
NSString *imgfileName = [NSString stringWithFormat:@"%@%@", gmtTime, @".jpg"];
// File we want to create in the documents directory
NSString *imgfilePath= [dataPath stringByAppendingPathComponent:imgfileName];
// Write the file
[imageData writeToFile:imgfilePath atomically:YES];
}

- (void)selectPhoto
{
self.isCamera = NO;

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/abc"];
self.photos = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:&error];

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.browser];
[self.browser reloadData];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:nc animated:YES completion:nil];
}
4

0 に答える 0