iOS中读取照片库及保存图片或视频到照片库的要点解析

2020-01-15 15:20:06于丽

                NSLog(@"Unknow AssetType");    
            }    
                
            NSDictionary *assetUrls = [result valueForProperty:ALAssetPropertyURLs];    
            NSUInteger assetCounter = 0;    
            for (NSString *assetURLKey in assetUrls) {    
                NSLog(@"Asset URL %lu = %@",(unsigned long)assetCounter,[assetUrls objectForKey:assetURLKey]);    
            }    
                
            NSLog(@"Representation Size = %lld",[[result defaultRepresentation]size]);    
        }];    
    } failureBlock:^(NSError *error) {    
        NSLog(@"Enumerate the asset groups failed.");    
    }];   

 

保存图片或视频到PhotoLibrary
时文件然后,然后通过临时文件的路径去转存到photo library。
我们直接来看相应的API:

复制代码
// These methods can be used to add photos or videos to the saved photos album.  
  
// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.  
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;  
  
// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image  
- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);  
  
// If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten