ios - 将相册中的照片添加到应用程序中以在 tableView 中显示 - IOS

标签 ios objective-c iphone uitableview uiimagepickercontroller

我目前正在开发一个应用程序,它允许我从我的相册中选择照片复制到我的应用程序中,以便在 tableView 中查看它们。基本上我有一个具有添加按钮和查看按钮的 View 。当按下添加按钮时,UIIMagePickerController 会显示照片。

一旦我选择了一张照片,我想将这张照片复制到我的本地应用程序。按下 View 按钮后,将加载一个 tableView,显示之前复制的所有照片。我现在被困在这部分。图像的实际复制和存储以供将来查看。

关于如何实现这一点有什么建议吗?如何创建一个 plist 并将文件添加到那里?

提前致谢!

T

最佳答案

如果您想将 UIIMagePickerController 中的图像保存到您的应用程序,那么最好的方法是使用您的文档目录文件夹,将这些图像保存到文档文件夹。

下面我将发布一些代码,帮助您保存和检索图像。

-(void) saveImageNamed :(NSString *) imageName andImage :(UIImage *) image{
      NSData *pngData = UIImagePNGRepresentation(image);
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
      NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
      NSString *filePath = [documentsPath stringByAppendingPathComponent:imageName]; //Add the file name
     [pngData writeToFile:filePath atomically:YES];
}

这是从文件夹中检索图像的方法

-(UIImage *) getImageWithPath :(NSString *) imagePath {
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
      NSString *documentsPath = [paths objectAtIndex:0];
      NSData *pngData = [NSData dataWithContentsOfFile:[documentsPath stringByAppendingPathComponent:imagePath]];
      UIImage *image = [UIImage imageWithData:pngData];
      return image;
}

希望对你有帮助。

关于ios - 将相册中的照片添加到应用程序中以在 tableView 中显示 - IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32194346/

相关文章:

objective-c - 从 Objective-C 函数返回二维 C 数组

iphone - Tapkulibrary 静态月

ios - UIActivity View Controller : Custom Activity Placement

ios - 如何在 iOS 中解析 JSON 数据

objective-c - 在 PLUICameraViewController 上设置 UIButtonLabel 上的文本

ios - 无效签名 : oauth_signature

ios - 如何从 UISearchBar 移动占位符标签

iphone - 应用委托(delegate)对象

Swiftui 渐变制作器 Spacer 无用

iphone - 我的自定义 MKAnnotationView 不可点击