UIImage轉(zhuǎn)PHAsset

網(wǎng)上很多PHAsset轉(zhuǎn)UIImage的代碼,沒有UIImage轉(zhuǎn)PHAsset的,今天有個同學(xué)剛好問,我正好研究一下,直接上代碼吧:

? ? UIImage*image = [UIImageimageNamed:@"image"];

? ? [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{

? ? ? ? PHAssetChangeRequest *request = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

? ? ? ? NSString *localIndentifier = request.placeholderForCreatedAsset.localIdentifier;

? ? ? ? PHFetchResult*assetResult = [PHAssetfetchAssetsWithLocalIdentifiers:@[localIndentifier]options:nil];

? ? ? ? PHAsset*asset = assetResult.firstObject;

? ? }error:nil];

另外需要注意,轉(zhuǎn)換代碼一定要寫在PHPhotoLibrary的

- (void)performChanges:(dispatch_block_t)changeBlock completionHandler:(nullable void(^)(BOOL success, NSError *__nullable error))completionHandler;

或者

- (BOOL)performChangesAndWait:(dispatch_block_t)changeBlock error:(NSError *__autoreleasing *)error;

中,不然會報異常:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]'

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容