使用 exif 将原始图像从 ALAsset 对象写入文档目录文件夹

Posted

技术标签:

【中文标题】使用 exif 将原始图像从 ALAsset 对象写入文档目录文件夹【英文标题】:Writing orignal image with exif to Document Directory folder from ALAsset object 【发布时间】:2013-05-14 12:09:27 【问题描述】:

我想用 EXIF 将 alasset imagearray 直接保存到文档目录 我试过PNG转换,jpeg转换没有用 它只是使用 jpg 或 png (丢失 exif)创建新图像

我已经看到有一段时间直接将 NSData 保存到文件夹以保存 EXIF 不知道如何

我正在使用

从 ALAsset 对象结果中获取元数据
NSDictionary *metadata = [[result defaultRepresentation] metadata];

另一个包含所有图像列表的资产数组

ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
        if(result != NULL) 
            [assets addObject:result];

            ;
          NSLog(@"assets %i",[assets count]);
            self.progressView.progress = (float)index / ([assets count]-1);
        

将图像保存到文档目录文件夹

-(void)saveImagesToDocumentDirectory
    NSLog(@"assets count %i",[assets count]);

    for(int i=0;i<[assets count];i++)
    
        currentImage = [UIImage imageWithCGImage:[[[assets objectAtIndex:i] defaultRepresentation] fullResolutionImage]];

        [self saveImage:currentImage withImageName:[NSString stringWithFormat:@"Images %d",i]];
            

    - (void)saveImage:(UIImage*)image withImageName:(NSString*)imageName 
        NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
      // NSData *imageData = UIImageJPEGRepresentation(image,1.0);
        NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
        NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"];
        NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
        [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

        NSLog(@"image saved");

【问题讨论】:

【参考方案1】:

这是我能够在exif保持原样的情况下将我的文档目录文件夹中的所有图像写入的方法,希望这对其他用户有所帮助

-(void)writingOutImage
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *documentdataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"];
    NSLog(@"documentdataPath %@",documentdataPath);
    for (int j=0; j<[assets count]; j++) 

        ALAssetRepresentation *representation = [[assets objectAtIndex:j] defaultRepresentation];
        NSString* filename = [documentdataPath stringByAppendingPathComponent:[representation filename]];

        [[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
        NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:filename append:YES];
        [outPutStream open];

        long long offset = 0;
        long long bytesRead = 0;

        NSError *error;
        uint8_t * buffer = malloc(131072);
        while (offset<[representation size] && [outPutStream hasSpaceAvailable]) 
            bytesRead = [representation getBytes:buffer fromOffset:offset length:131072 error:&error];
            [outPutStream write:buffer maxLength:bytesRead];
            offset = offset+bytesRead;
        
        [outPutStream close];
        free(buffer);
    

【讨论】:

以上是关于使用 exif 将原始图像从 ALAsset 对象写入文档目录文件夹的主要内容,如果未能解决你的问题,请参考以下文章

ALAsset Library 减小图像的大小,同时将其保存在照片库中

ALAsset 图像大小

从 ALAsset 检索添加到库的日期?

ALAsset 将资源转换为 NSData

iPhone OS 图像/照片大小调整工具更改 EXIF 方向数据

带有 ALAsset 缩略图的低清晰度