Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将图像文件从iOS照片库复制到应用程序的本地目录?_Ios_Image_Photos_Alassetslibrary_Alasset - Fatal编程技术网

如何将图像文件从iOS照片库复制到应用程序的本地目录?

如何将图像文件从iOS照片库复制到应用程序的本地目录?,ios,image,photos,alassetslibrary,alasset,Ios,Image,Photos,Alassetslibrary,Alasset,我可以通过照片库获取图像: void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){ if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) { // Copy the photo imag

我可以通过照片库获取图像:

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop){
        if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
            // Copy the photo image to the `/Documents` directory of this App here

        }
    };

    void (^assetGroupEnumerator )(ALAssetsGroup*, BOOL*) = ^(ALAssetsGroup *group, BOOL *stop){
        if (group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
    };
    // fetch
    ALAssetsLibrary *library = [ALAssetsLibrary new];
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:^(NSError *error) {
        NSLog(@"failed");
    }];

我想将特定图像复制到本地目录(
App\u home/Documents
),但我不知道如何通过处理ALAsset对象来准确地完成这项工作。

请尝试以下代码

ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:YourURL resultBlock:^(ALAsset *asset) 
    {
       ALAssetRepresentation *rep = [asset defaultRepresentation];
       Byte *buffer = (Byte*)malloc(rep.size);
       NSUInteger buffered = [rep getBytes:buffer fromOffset:0 length:rep.size error:nil];
       NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
      [data writeToFile:photoFile atomically:YES];//you can save image later
   }
    failureBlock:^(NSError *err) 
    {
      NSLog(@"Error: %@",[err localizedDescription]);

    }
];
用于在文档目录中获取图像

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *newPath = [documentsDirectory stringByAppendingPathComponent:@"Your_Image_Name"];
UIImage *myImg = [UIImage imageWithContentsOfFile:newPath]

这可能对你有帮助。在这种情况下,outputFileURL的类型为NSURL

   NSData *videoData = [NSData dataWithContentsOfURL:outputFileURL];

  [data writeToFile:destinationPath atomically:YES];//you can save image later

您可以使用下面的实现获取照片原始二进制文件并保存到目标文件

+ (NSData *)photoAssetRawData:(ALAsset *)photoAsset error:(NSError **)error {
    ALAssetRepresentation *rep = photoAsset.defaultRepresentation;
    NSMutableData *data = [NSMutableData new];
    long long offset = 0;
    uint8_t dataBuffer[PHOTO_READ_CHUNK_SIZE];
    NSError *internalError;
    do {
        NSUInteger readByteLength = [rep getBytes:dataBuffer fromOffset:offset length:sizeof(dataBuffer) error:&internalError];
        if(internalError != nil) {
            if(error != NULL) {
                *error = internalError;
            }
            return nil;
        }
        offset += readByteLength;
        [data appendBytes:(void*)dataBuffer length:readByteLength];
    }
    while (offset < rep.size);
    return data;
}

在ALAssets方法中是否有任何NSURL类型的变量?请尝试粘贴ALASSETLibrary方法代码。我将按GUID文件名将这些图像保存在
文档
目录中,扩展名为.png。谢谢,我将尝试此操作。但是我怎样才能恢复图像呢?使用
UIImage*image=[UIImage-imagename:photoFile]?是的,获取我保存在文档目录中的图像。我可以通过iOS文件系统将文件复制到我的应用程序的
文档
目录吗@李岡諭 沙盒阻止您获取“真实的”
文件://
urlCool,它看起来更简单。我真的需要创建AlassetLibrary的实例吗?似乎需要使用AlassetLibrary在照片库中获取图像。因此,您的方法可能不起作用。
+ (CGImageRef)applyXMPFilter:(ALAsset *)asset{
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef imageRef = [rep fullResolutionImage];
    NSString *adjustmentXMP;
    NSData *adjustmentXMPData;
    NSError *__autoreleasing error = nil;
    NSArray *filters=nil;
    CGRect extend = CGRectZero;
    //add filter to image
    ALAssetRepresentation *representation = asset.defaultRepresentation;
    adjustmentXMP = [representation.metadata objectForKey:@"AdjustmentXMP"];
    adjustmentXMPData = [adjustmentXMP dataUsingEncoding:NSUTF8StringEncoding];
    extend.size = representation.dimensions;
    filters = [CIFilter filterArrayFromSerializedXMP:adjustmentXMPData inputImageExtent:extend error:&error];
    if(filters)
    {
        CIImage *image = [CIImage imageWithCGImage:imageRef];
        CIContext *context = [CIContext contextWithOptions:nil];
        for (CIFilter *filter in filters)
        {
            [filter setValue:image forKey:kCIInputImageKey];
            image = [filter outputImage];
        }
        imageRef = [context createCGImage:image fromRect:image.extent];
    }
    return imageRef;
}