2013-05-14 61 views
2

我想直接保存alasset imagearray到文檔目錄與EXIF 我試過PNG轉換,JPEG轉換沒有什麼工作 它只是創造新形象要麼JPG或PNG (EXIF的損失)從ALAsset對象寫入與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); 
    } 
} 
相關問題