All the online document and samples showed how to edit/change assets with PHContentEditingInput and PHContentEditingOutput. I didn't find anything about resetting or reverting an image to its original. Anything wrote to renderedContentURL is considered an edit, so that's not what I want. Just share my findings here:
Use revertAssetContentToOriginal
Swift:
PHPhotoLibrary.shared().performChanges({
let request = PHAssetChangeRequest(for:asset)
request.revertAssetContentToOriginal()
}, completionHandler: { success, error in
if !success { print("can't revert asset: \(error)") }
})
Objective C:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *change = [PHAssetChangeRequest changeRequestForAsset:asset];
[change revertAssetContentToOriginal];
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished adding asset. %#", (success ? #"Success" : error));
}];
Related
I'm just use the PhotoKit to delete photos, just like this:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets: arrayOfPHAssets];
} completionHandler:^(BOOL success, NSError *error) {
NSLog(#"Finished deleting asset. %#", (success ? #"Success." : error));
}];
And the result is:delete image's info
How to change the title's language to Chinese.
I have a try to change iPhone's language and it doesn't matter.Please.
My application has function to downloads many photo and video files in tmp folder and save them in camera roll with PHPhotoLibrary API.
The problem is that sometimes (the probability is around 10%) exception happens in the saving process.
The error message in console is,
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This method can only be
called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary
performChangesAndWait:error:]'
My code is like below:
- (void)saveVideoFileInCameraRoll:(NSString *)videoFilePath
{
NSURL *videoFileUrl = [NSURL fileURLWithPath:videoFilePath];
photoLibrarySaveImageCompletion completion = ^(BOOL success, NSError *error) {
NSLog(#"success=%#, error=%#", (success ? #"YES" : #"NO"), error);
};
NSLog(#"videoFileUrl=%#", videoFileUrl);
[self saveVideoFile:videoFileUrl completion:completion];
}
- (void)saveVideoFile:(NSURL *)fileURL completion:(photoLibrarySaveImageCompletion)completion
{
NSLog(#"fileURL=%#", fileURL);
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
NSLog(#"fileURL=%#", fileURL);
// Exception happens on this line as [SIGABRT]
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:fileURL];
if (_assetCollection) {
PHAssetCollectionChangeRequest *assetCollectionChangeRequest =
[PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
[assetCollectionChangeRequest addAssets:#[ [assetChangeRequest placeholderForCreatedAsset] ]];
}
else {
NSLog(#"### assetCollection is nil ###");
}
}
completionHandler:^(BOOL success, NSError *_Nullable error) {
NSLog(#"success=%#, error=%#", (success ? #"YES" : #"NO"), error);
completion(success, error);
}];
}
I checked similar case:
Save image to photo library using photo framework
The case crashes every time, but my code rarely crashes.
And unlike the case I'm calling
[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:]
in 'performChanges' block
Also I confirmed the fileURL of video file in tmp folder by NSLog and it's OK outside and inside of 'performChanges' block.
fileURL=file:///private/var/mobile/Containers/Data/Application/C87F0F75-E128-4E9F-AE07-6B914939AC5D/tmp/video3.mp4
I would appreciate it if you would let me know the reason or resolution of this issue.
I am trying to remove a photo from assets using setImageData:nil method like below:
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
NSLog(#"asset url: %#", asset.defaultRepresentation.url);
if(asset.isEditable) {
[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Asset url %# should be deleted. (Error %#)", assetURL, error);
}];
}
}];
The photo has been saved with the same app so I understand this should be possible, but it is not removed from the photo library and it does not give any error. The first NSLog prints the correct assetURL but when setImageData is called the assetURL is null.
asset url: assets-library://asset/asset.PNG?id=4B782AD1-30CF-4EDD-8879-CE262E6BE884&ext=PNG
Asset url (null) should be deleted. (Error (null))
Using this method to delete an image has been deprecated in recent iOS releases or what is the cause of it not working?
I want to programmatically remove assets from an album which is a duplicate (the photos are not). I can delete the album using Photos framework
I want to know how to remove the asset from the album without deleting it completely from photos app. There are multiple places where I want to use this, eg to move asset from one album to another, etc
The albums are editable as I have created them.
Is there a way to do this in devices with iOS 6 or iOS 7?
In iOS 8 and later you can use following code to remove a photo from an album without deleting the photo completely from photos app.
[[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));
}];
The code is provided at following link:
https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetCollectionChangeRequest_Class/index.html
Swift 4 version of Tgiri's answer if it's helpful for anyone:
// you want to pass in your asset collection and asset
let album: PHAssetCollection
let asset: PHAsset
PHPhotoLibrary.shared().performChanges({
guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album) else { return }
let fastEnumeration = NSArray(array: [asset])
albumChangeRequest.removeAssets(fastEnumeration)
}, completionHandler: { success, error in
if success {
print("removed")
} else {
print("not removed")
}
})
You can set the imageData of that image to nil, in that way you can delete an image.
ALAssetsLibrary *lib = [ALAssetsLibrary new];
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if(group == nil)
{
return ;
}
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:#"YourAlbumNam"])
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if(asset.isEditable)
{
if([[asset valueForProperty:ALAssetPropertyAssetURL] isEqual:yourAssetURL] )
{
[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error)
{
// Check for error
}];
}
}
}];
}
failureBlock:^(NSError *error)
{
}];
I want to save all image's from my app with new album(with my app name). so I have use asserts library in my project. it is working good in ios 7 but not in ios8 and later one.With Mistake when user remove album from photos,assert library unable to create new album again with same name in ios8. Any one has solution for this? Thanks
You can try My below Method for Create Album for iOS 7 and iOS 8
#define PHOTO_ALBUM_NAME #"AlbumName Videos"
#pragma mark - Create Album
-(void)createAlbum{
// PHPhotoLibrary_class will only be non-nil on iOS 8.x.x
Class PHPhotoLibrary_class = NSClassFromString(#"PHPhotoLibrary");
if (PHPhotoLibrary_class) {
// iOS 8..x. . code that has to be called dynamically at runtime and will not link on iOS 7.x.x ...
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:PHOTO_ALBUM_NAME];
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(#"Error creating album: %#", error);
}else{
NSLog(#"Created");
}
}];
}else{
[self.library addAssetsGroupAlbumWithName:PHOTO_ALBUM_NAME resultBlock:^(ALAssetsGroup *group) {
NSLog(#"adding album:'Compressed Videos', success: %s", group.editable ? "YES" : "NO");
if (group.editable == NO) {
}
} failureBlock:^(NSError *error) {
NSLog(#"error adding album");
}];
}}