PHAssetResourceCreationOptions file name

I am moving videos to camera roll using new PHPhotoLibrary APIs in iOS 9. But the problem is the name of the file in camera roll is random and begins with anything from A-H. This creates problem for users when they try importing videos from camera roll and see videos much before all photos they have taken (which are named as IMG_****). Why does PHKit creates a random file name when renaming as opposed to consistent file names beginning with IMG*** if one uses ALAssetsLibrary API ? What is the workaround to get names starting with IMG_ ?

Replies

You can set a custom file name using AssetsLibrary or PhotoKit on iOS 8. On Import the filename is always set to the form "IMG.***".


Using iOS 9 or higher you can use


[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
     NSURL *url;
     PHAssetResourceType assetType = PHAssetResourceTypePhoto;
     PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
     PHAssetResourceCreationOptions *creationOptions = [PHAssetResourceCreationOptions new];
     creationOptions.originalFilename = @"customname.jpg";
     [request addResourceWithType:assetType fileURL:url options:creationOptions];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
}];


This will set an explicit custom filename. If you use the iOS 8 based PhotoKit APIs to import an asset you should get the behaviour as under iOS 8 (IMG_***). If the behaviour is non consistent, i would report it as a bug.

I try this but the filename is ramdom always. iOS 9.3.2 with Swift 2

Did anyone at the end solved the problem with weird naming of files in camera roll when Photo API is used? If you set the "originalFilename" to string in format "IMG_XXXX.JPG" it actually works (if string has more than 8 symbols before extension, then name become again random), except now I have a problem to find the right XXXX number. Any help will be highly appreciated