【问题标题】:How to save the image in local in applications如何在应用程序中将图像保存在本地
【发布时间】:2016-05-14 02:00:21
【问题描述】:

您好,我正在开发一个基于聊天的应用程序,现在我想在本地保存图像,因为我已经使用以下代码在设备中本地创建了一个目录“My_Videos”。

 NSString *albumName=@"My_Videos";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:albumName
                         resultBlock:^(ALAssetsGroup *group) {
                             NSLog(@"added album:%@", albumName);
                         }
                        failureBlock:^(NSError *error) {
                            NSLog(@"error adding album");
                        }];

它正在成功创建文件夹但是现在我想从 Web API 下载图像并保存在我在设备中创建的“My_Videos”文件夹中。 请帮帮我。

【问题讨论】:

  • 我猜你只能保存在你的文档目录中,如果我错了,请纠正我
  • 是但我想保存在指定的floder中
  • 您可以将图像/视频保存到您的相机胶卷相册中。我相信没有办法在你的相册中选择一个特定的文件夹。看看这个:developer.apple.com/library/ios/documentation/UIKit/Reference/…
  • @iOSAddicted 感谢重播,但有些应用程序有这个功能对吗?所以我正在尝试,如果您知道其他任何事情,请告诉我
  • @iosAddicted 请接受这个问题,如果你认为它会使用完整的,它会使用完整的其他身体

标签: ios uiimage nsdocumentdirectory


【解决方案1】:

您在 AssetsLibrary 中创建新组的方向正确。以下是添加照片的方法:

首先您需要将照片写入标准资源库(相机胶卷)

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:image.CGImage
                          orientation:(ALAssetOrientation)image.imageOrientation
                      completionBlock:^(NSURL* assetURL, NSError* error)
 {
     if (!error) {

         __block BOOL albumAlredyCreated = NO;

         // Check if Album already exists
         [library enumerateGroupsWithTypes:ALAssetsGroupAlbum
                                usingBlock:^(ALAssetsGroup *group, BOOL *stop)
          {
              if ([customAlbum compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame)
              {
                  albumAlredyCreated = YES;

                  //Get the ALAsset instance for the saved image
                  [library assetForURL: assetURL
                           resultBlock:^(ALAsset *asset)
                   {

                       //Save photo to customAlbum
                       [group addAsset: asset];

                   } failureBlock:^(NSError *error) {

                       // If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
                       // If the data is currently unavailable, the failure block will be called.
                   }];
              }

              // Album does not exist. Create it
              if (group == nil && albumAlredyCreated == NO)
              {
                  __weak ALAssetsLibrary* weakLibrary = library;

                  [library addAssetsGroupAlbumWithName:customAlbum
                                           resultBlock:^(ALAssetsGroup *group)
                   {

                       //Get the ALAsset instance for the saved image
                       [weakLibrary assetForURL: assetURL
                                    resultBlock:^(ALAsset *asset) {

                                        //Save photo to customAlbum
                                        [group addAsset: asset];

                                    } failureBlock:^(NSError *error) {

                                        // If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
                                        // If the data is currently unavailable, the failure block will be called.
                                    }];
                   } failureBlock:^(NSError *error) {

                       // If the user denies access to the application or if no application is allowed to access the data, the failure block will be called.
                   }];
              }

          } failureBlock:^(NSError *error) {

              // If the user denies access to the application, or if no application is allowed to access the data, the failureBlock is called.
          }];
     }
 }];

虽然这可行,但 ALAssetsLibrary 在 iOS 9 中已被弃用,因此您可能需要考虑移至 PHPhotoLibrary。

NS_CLASS_DEPRECATED_IOS(4_0, 9_0, "使用照片中的 PHPhotoLibrary 框架代替") @interface ALAssetsLibrary : NSObject

【讨论】:

  • 感谢重播,但我只想保存图像到现在你能告诉我怎么做吗?
  • 感谢重播,我收到此错误“与 assetsd 的连接被中断或 assetsd 死亡”我错过了什么?
  • 尝试重置模拟器。这个问题已经问过here
猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2014-01-14
  • 1970-01-01
  • 2013-02-02
相关资源
最近更新 更多