简体   繁体   English

将带有exif的原始图像从ALAsset对象写入Document Directory文件夹

[英]Writing orignal image with exif to Document Directory folder from ALAsset object

I want to save alasset imagearray directly to document directory with EXIF i tried PNG conversion, jpeg conversion nothing worked It just creating new image either with jpg or png (loss of exif) 我想使用EXIF将a集图像数组直接保存到文档目录中,但我尝试了PNG转换,jpeg转换却无济于事它只是用jpg或png创建新图像(exif丢失)

I have seen some time back to save NSData Directly to folder to preserve EXIF dont know how 我看过一段时间将NSData直接保存到文件夹以保存EXIF不知道如何

I am getting metadata from ALAsset object result with 我正在从ALAsset对象结果中获取元数据

NSDictionary *metadata = [[result defaultRepresentation] metadata];

Another assets array with list of all images 包含所有图像列表的另一个资产数组

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);
        }

Saving images to document directory folder 将图像保存到文档目录文件夹

-(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");
}

This is the method i am able to write all images in my document directory folder with exif remain intact, hope this will help other users 这是我可以在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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM