How to copy image file from iOS Photo Library (ALAssetsLibrary) to the local application directory?

I can get images from the photo library through ALAssetsLibrary:

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

I want to copy certain images to a local directory ( App_home/Documents ), but I don’t know how to exactly do this work by processing ALAsset objects.

+4
source share
3 answers

Try the following code

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

To obtain an image in the document directory

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

This may be useful for you. In this outputFileURL is of type NSURL

  NSData *videoData = [NSData dataWithContentsOfURL:outputFileURL]; [data writeToFile:destinationPath atomically:YES];//you can save image later 
0
source

You can get the source binary file with the photo below and save it in the target file.

 + (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; } 

One thing to keep in mind, this raw data did not apply any default filter gallery for iOS filters. If you want this filter to be applied, you should get this XMP-enabled filter from the [ALAssetRepresentation] metadata and create filters using [CIFilter filterArrayFromSerializedXMP: inputImageExtent: error:], then apply them at full resolution, finally save this processed image as JPEG or PNG to file.

The following shows how to apply these filters.

 + (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; } 
0
source

Source: https://habr.com/ru/post/1501587/


All Articles