How can delete one video from asset/album in iOS8
When I use UIImagePickerController for purpose of selection
then I select one Video from asset/album
in Info Dictionary I got response on didFinishPickingMediaWithInfo method
{
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerMediaURL = "file:///Users/appaspect2/Library/Developer/CoreSimulator/Devices/A61DFE3B-8BA0-4A18-939A-2B0BC2F6084E/data/Containers/Data/Application/69E55783-CB4F-4620-9C81-B3948F5A843B/tmp/trim.1202C1FA-6130-4BC7-AA31-391CC7E0D7B6.MOV";
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.MOV?id=189B68BD-17D0-4665-A065-11A40B0F2B25&ext=MOV";
}
I want to delete file this file which is select in UIImagePickerControllerReferenceURL or asset/album video
AssestsLibrary do not have any such method. But after searching about this I found in ios8 deleting photos might be possible using the Photos Framework
Check the documentation of Photos Framework
For deleting assets refer to PHAssetChangeRequest
+ (void)deleteAssets:(id<NSFastEnumeration>)assets
Finally so much effort I am enable to delete file of asset/album video for iOS8 Only.
You can add this in didFinishPickingMediaWithInfo Method
PHFetchResult *asset = [[PHAsset fetchAssetsWithALAssetURLs:#[info[#"UIImagePickerControllerReferenceURL"]] options:nil] firstObject];
NSLog(#"asset %#",asset);
[self toggleFavoriteForAsset:(PHAsset *)asset];
and Add One Method For Delete Asset Video
- (void)toggleFavoriteForAsset:(PHAsset *)asset {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Create a change request from the asset to be modified.
BOOL req = [asset canPerformEditOperation:PHAssetEditOperationDelete];
if (req) {
NSLog(#"true");
[PHAssetChangeRequest deleteAssets:#[asset]];
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished Delete asset. %#", (success ? #"Success." : error));
}];
}
Related
In iOS 10 I start to replace asset-library to PhotoKit to manage image picker. But there is an issue.
The specific step is that app needs system camera and user shot a photo after that the delegate method imagePickerController:didFinishPickingMediaWithInfo: will be called.
Then, here is my code:
UIImage *pickerImage = [[info objectForKey:UIImagePickerControllerOriginalImage] fixOrientation];
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
__block NSString *localId;
[photoLibrary performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:pickerImage];
localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
// to get localIdentifier and reload collectionView
}
}];
line 1 that fixOrientation is a custom category that return UIImage instance.
Only call [PHAssetChangeRequest creationRequestForAssetFromImage:] method performs fine. However, when I want to fetch newly created photo to use [assetChangeRequest placeholderForCreatedAsset] , it shows memory leak and app crash.
Above all, any solution to fetch created photo just moment or other methods to solve that by using Photos Framework?
I'm changing over from ALAssetsLibrary to PHPhotoLibrary and want to know whether or not video file can save or not.
In ALAssetsLibray, you know, we could know it before start saving video file with the API
- (BOOL)videoAtPathIsCompatibleWithSavedPhotosAlbum:(NSURL *)videoPathURL;
Does anyone knows the substitute API for videoAtPathIsCompatibleWIthSavedPhotoAlbum in PHPhotoLibrary ?
Or do you know the exact error code which means that a video file cannot save on the iOS device ?
My code for saving video file is like below,
- (void)saveVideoFile:(NSURL *)videoURL completion:(savePhotoFileCompletion)completion
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest;
assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL];
PHAssetCollectionChangeRequest *assetCollectionChangeRequest =
[PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
[assetCollectionChangeRequest addAssets:#[ [assetChangeRequest placeholderForCreatedAsset] ]];
}
completionHandler:^(BOOL success, NSError *_Nullable error) {
DBGLog(#"success=%#, error=%#", (success ? #"YES" : #"NO"), error);
completion(success, error);
}];
}
I found the question talking about the same issue.
How to use PHPhotoLibrary like ALAssetsLibrary
But it doesn't refer to the exact answer.
I asked my question to Apple with TSI.
And their answer is to use the following API
AVAsset *avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:videoPath]];
BOOL canSaveVideo = [avAsset isCompatibleWithSavedPhotosAlbum];
It seemed that I could get the correct compatibility.
One important point it that PHPhotoLibrary can save the video even if the value of canSaveVideo is NO.
Thank you.
I have PHAssetCollection in my application which contain all albums of photos app.
Problem is that i can delete images from the All Photos but cant delete images from camera roll,favourites,screenshots etc..
Plz help me to delete from PHAssetCollection.
this is the code i did to delete images.
// Enable the trash button if the asset can be deleted.
BOOL isTrashable = NO;
if (self.assetCollection) {
isTrashable = [self.assetCollection canPerformEditOperation:PHCollectionEditOperationRemoveContent];
}
else {
isTrashable = [self.asset canPerformEditOperation:PHAssetEditOperationDelete];
}
self.trashButton.enabled = isTrashable;
[self updateImage];
reference from Apple developer forum
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest
changeRequestForAssetCollection:self.myAlbum
assets:self.albumAssetsFetchResult];
[request removeAssets:#[asset]];
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished removing asset from the album. %#", (success ? #"Success" : error));
}];
or
// Delete asset from library
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:#[self.asset]];
} completionHandler:completionHandler];
make sure isTrashable = YES
Comment the above code and set
self.trashbutton.enable=YES;
Images deleted from camera role also deleted from All Photos.
I am developing a camera application which can be used to take pictures and save them in a separate album. I used photos framework to save images and now I need to save GPS data (location where the picture is taken) with the picture (may be in metadata). I searched for any method to do this thing using photos framework but I failed, I couldn't find anything related. Any help would be highly appreciated.
This is the peace of code I used to save pictures
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:capturedImage];
placeholder = [assetRequest placeholderForCreatedAsset];
photosAsset = [PHAsset fetchAssetsInAssetCollection:Album options:nil];
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:Album assets:photosAsset];
[albumChangeRequest addAssets:#[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
}
else
{
}
}];
I assume you want the latitude & longitude. Have you tried the location property of PHAsset class?
When a picture is taken (iOS 8.1) and didFinishPickingMediaWithInfo is called, I'm trying to use PhotoKit to add the GPS data back into the metadata.
This is the code I'm using:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
newAssetRequest.creationDate = [NSDate date];
newAssetRequest.location = [self deviceLocation];
NSLog(#"didFinishPickingMediaWithInfo: location:%#", newAssetRequest.location);
PHObjectPlaceholder *placeholderAsset = newAssetRequest.placeholderForCreatedAsset;
PHAssetCollectionChangeRequest *addAssetRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.myResults[0]];
[addAssetRequest addAssets:#[placeholderAsset]];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"didFinishPickingMediaWithInfo: Success saving picture to album");
} else {
NSLog(#"didFinishPickingMediaWithInfo: error saving picture to album %#", error);
}
}];
[picker dismissViewControllerAnimated:YES completion:nil];
The code takes the image and moves it to an album. This part works. The photo ends up in the correct album.
The problem is the GPS data is not present in the metadata even though the location property is properly set. I know the location data is valid.
Shouldn't this work? Is there an alternate approach to get the desired result? I don't really care about all the other metadata, just the GPS coordinates.