实用技术

PhotoKit

2016-09-25  本文已影响58人  wpf_register

参考文档1
参考文档2
参考文档3

上文是用QBImagePickerController和UIImagePickerController 非常简陋地实现了图片多选。现再码一下图片相关要点。

1.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  
  //选择的图片
info{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //修改后的图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    //原件的URL
    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
     //媒体类型
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
 }
//选择视频
info{
    //原件的URL
    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    //视频URL
    NSURL  * mediaURL = [info objectForKey:UIImagePickerControllerMediaURL]
    //媒体类型
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  }
//选择LivePohoto
info{
     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
     PHLivePhoto *livePhot = [info objectForKey: UIImagePickerControllerLivePhoto];
    //原件的URL
      NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
     //媒体类型
     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
}

图片,视频和LivePhoto三种回调信息有差别,
但都有UIImagePickerControllerReferenceURL,可以从中得到PHAsset

NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
PHAsset *asset = fetchResult.firstObject;

    //媒体类型
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    //媒体类型分为两类
    //务必导入 #import <MobileCoreServices/MobileCoreServices.h>
    //并进行转换 如:(__bridge NSString*)kUTTypePNG转换成字符串类型
    //kUTTypeImage
    //const CFStringRef  kUTTypeImage ;抽象的图片类型
    //const CFStringRef  kUTTypeJPEG ;
    //const CFStringRef  kUTTypeJPEG2000 ;
    //const CFStringRef  kUTTypeTIFF ;
    //const CFStringRef  kUTTypePICT ;
    //const CFStringRef  kUTTypeGIF ;
    //const CFStringRef  kUTTypePNG ;
    //const CFStringRef  kUTTypeQuickTimeImage ;
    //const CFStringRef  kUTTypeAppleICNS
    //const CFStringRef  kUTTypeBMP;
    //const CFStringRef  kUTTypeICO;

    //KUTTypeMovie
    //const CFStringRef  kUTTypeAudiovisualContent ;抽象的声音视频
    //const CFStringRef  kUTTypeMovie ;抽象的媒体格式(声音和视频)
    //const CFStringRef  kUTTypeVideo ;只有视频没有声音
    //const CFStringRef  kUTTypeAudio ;只有声音没有视频
    //const CFStringRef  kUTTypeQuickTimeMovie ;
    //const CFStringRef  kUTTypeMPEG ;
    //const CFStringRef  kUTTypeMPEG4 ;
    //const CFStringRef  kUTTypeMP3 ;
    //const CFStringRef  kUTTypeMPEG4Audio ;
    //const CFStringRef  kUTTypeAppleProtectedMPEG4Audio;

}

PS :
CFStringRef ----> NSStirng
NSString yourFriendlyNSString = (__bridge NSString )yourFriendlyCFString;
NSString ---> CFStringRef
CFStringRef yourFriendlyCFString = (__bridge CFStringRef)yourFriendlyNSString;

2.

通过UIImagePickerViewController得拍到的照片并没保存,所以需要进行手动保存。

  //保存到相簿
 //保存完回调方法可置nil 

 UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);//保存到相簿

 NSURL *url=[infoobjectForKey:UIImagePickerControllerMediaURL];//视频路径
 NSString *urlStr=[url path];
 UISaveVideoAtPathToSavedPhotosAlbum(urlStr,self, @selector(video:didFinishSavingWithError:contextInfo:),nil);//保存视频到相簿

3.

import <AssetsLibrary/AssetsLibrary.h>

//使用 ALAsset 需要引入 AssetsLibrary。 AssetsLibrary 在 iOS9.0 已经过期。

import <Photos/Photos.h>

//使用 PHAsset 需要引入Photos Framework,支持 iOS8.0 及以上版本。
所以我们需要根据系统版本不同做判断处理。

上一篇下一篇

猜你喜欢

热点阅读