如何在应用程序中保存本地图像

时间:2016-02-04 12:42:57

标签: ios uiimage nsdocumentdirectory

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

function onLoad(){

    document.title = App.Properties.__caption;  
    document.getElementById('blockName').innerText = App.Properties.__caption;
    getAllRecords();
};

function getAllRecords(){

    //Get all the records available in the excel building block

    window.App.Core.getRecords(function(products){

        var product ='';

        for(var i = 0; i < products.length; i++){

            //Get the current record

            product = products[i];

            document.write(JSON.stringify(product, ['name', 'description','imageid', 'price', 'qty', 'shop']));
        }
    });
};

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

1 个答案:

答案 0 :(得分: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.
          }];
     }
 }];

虽然这可行,但{9}已在iOS 9中进行了停用,因此您可能需要考虑转移到ALAssetsLibrary

  

NS_CLASS_DEPRECATED_IOS(4_0,9_0,“使用照片中的PHPhotoLibrary   而不是框架“)@interface ALAssetsLibrary:NSObject