image
image
1.PHAsset获取本地图片的信息:名称,大小
// 名称
PHAsset* asset;
NSString *fileName = @"";
if (asset) {
fileName = [asset valueForKey:@"filename"];
}
// 大小
UIImage* image;
NSData* data = UIImageJPEGRepresentation(image, 1);
NSInteger size = [data length];
2.对于拍照 获取本地图片的信息:名称,大小
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"%@", info);
[picker dismissViewControllerAnimated:YES completion:nil];
// info中就包含了选择的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.avatarImageView.image = image;
// 先保存到相册 然后获取 PHAsset
// saveImageToPhotoLibrary - 已经写YMImage分类
self.asset = [image saveImageToPhotoLibrary][0];
}
YMImage分类
/**
保存图片到本地,并读取本地图片信息
相当于 - (PHFetchResult<PHAsset *> *)saveImageToPhotoLibrary
*/
- (void)saveImageToPhotoLibraryFinish:(void(^)(PHAsset *asset))finish {
NSMutableArray *imageIds = [NSMutableArray array];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//写入图片到相册
PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:self];
//记录本地标识,等待完成后取到相册中的图片对象
[imageIds addObject:req.placeholderForCreatedAsset.localIdentifier];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
//成功后取相册中的图片对象
PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:imageIds options:nil];
if (finish) finish([result firstObject]);
}
}];
}
/**
获得刚才添加到【相机胶卷】中的图片
相当于 - (void)saveImageToPhotoLibraryFinish:(void(^)(PHAsset *asset))finish
*/
- (PHFetchResult<PHAsset *> *)saveImageToPhotoLibrary {
__block NSString *createdAssetId = nil;
// 添加图片到【相机胶卷】
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdAssetId = [PHAssetChangeRequest creationRequestForAssetFromImage:self].placeholderForCreatedAsset.localIdentifier;
} error:nil];
if (createdAssetId == nil) return nil;
// 在保存完毕后取出图片
return [PHAsset fetchAssetsWithLocalIdentifiers:@[createdAssetId] options:nil];
}
3.选择视频
#import <MobileCoreServices/MobileCoreServices.h>
[self openImagePickerController:UIImagePickerControllerSourceTypePhotoLibrary];
- (void)openImagePickerController:(UIImagePickerControllerSourceType)type
{
if (![UIImagePickerController isSourceTypeAvailable:type]) return;
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = type;
ipc.mediaTypes = [NSArray arrayWithObject:(__bridge NSString*)kUTTypeMovie];
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"%@", info);
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(__bridge NSString*)kUTTypeMovie]){
if (@available(iOS 11.0, *)) {
PHAsset *asset = info[UIImagePickerControllerPHAsset];
NSString *fileName = @"";
if (asset) {
fileName = [asset valueForKey:@"filename"];
NSLog(@"%@", fileName);
}
} else {
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *fileName = [obj valueForKey:@"filename"];
if (fileName.length) {
*stop = YES;
}
NSLog(@"%@", fileName);
}];
}
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
网友评论