Hi I'm playing with the new photos framework for ios 8.0. I'm trying to delete an array of photos and here is the code:
NSArray *toDeletePhotos = [photos valueForKey:#"asset"];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest deleteAssets:toDeletePhotos];
} completionHandler:^(BOOL success, NSError *error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self refreshPhotosAfterDeleting];
});
}
}];
I tested this on around 8 devices. 6 of them successfully deleted selected photos and 2 of them returned and error that says: Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)" The two devices I tested on are 6+ and 5s.
I couldn't figure out what error it is and wonder anyone could help me with this. Thanks!
so after a while I solved the problem my self.
It turns out that when photos are streamed/synced from other devices, there's no way you can delete them without deleting them on iTunes/iCoud. So I added a filter so no streamed/synced photos are fetched.
For more information please refer to: https://support.apple.com/en-us/HT204120.
Hope this helps!
Related
I am trying to create a collection list in Photos which will have multiple albums.
The code to create the collection list is as follows:
__block NSString * localId;
NSError *createFolderError;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// Create the folder
PHCollectionListChangeRequest *changeRequest = [PHCollectionListChangeRequest creationRequestForCollectionListWithTitle: #"My Photos"];
localId = [[changeRequest placeholderForCreatedCollectionList] localIdentifier];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (!success) {
DebugLog(#"Creating My Photos Folder Failed... error : %#", error.description);
return;
}
if(localId) {
//Do something
}
}];
But, getting error as follows:
Error Domain=NSCocoaErrorDomain Code=-1 "(null)"
Earlier, it was creating folder even after getting the error, but now, I see folder not getting created under Photos application.
Let me know if I am doing anything wrong.
EDIT
The folder is getting created in the Photos app each time, but was not reflecting. When Photos App was killed and relaunched, I can see the folder. But still, why the error is shown, remains a question.
I am using setImageData to delete a photo in the following way :
[asset setImageData:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error)
{
// Do something
}];
This code was working perfectly fine in iOS 8.2 and earlier versions.
But, in 8.3 it gives the error :
#"Error Domain=ALAssetsLibraryErrorDomain Code=-3311 \"User denied access\" UserInfo=0x175061ac0 {NSLocalizedFailureReason=The user has denied the application access to their media., NSLocalizedDescription=User denied access, NSUnderlyingError=0x17025d700 \"The operation couldn’t be completed. (ALAssetsLibraryErrorDomain error -3311.)\”}"
I tried replacing the image data and metadata fields with some valid image data instead of “nil”. Still it gives the same error!!
Is this some bug in iOS 8.3? Is there any workaround?
Thanks in anticipation.
Another important information :
[PHPhotoLibrary authorizationStatus] returns "PHAuthorizationStatusAuthorized".
[ALAssetsLibrary authorizationStatus] also returns "ALAuthorizationStatusAuthorized".
As far as I know, the setImageData method was never intended to be used as a method for deleting assets. It is possible that on iOS 8.3 Apple patched things up so this no longer works.
I recommend that you look into using the Photos framework which includes a dedicated method for deleting assets.
Here's an example:
-(void)deleteAssetWithURL:(NSString*)assetURLString
{
NSURL *assetURL = [NSURL URLWithString:assetURLString];
if (assetURL == nil)
{
return;
}
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:#[assetURL] options:nil];
if (result.count > 0)
{
PHAsset *phAsset = result.firstObject;
if ((phAsset != nil) && ([phAsset canPerformEditOperation:PHAssetEditOperationDelete]))
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
{
[PHAssetChangeRequest deleteAssets:#[phAsset]];
}
completionHandler:^(BOOL success, NSError *error)
{
if ((!success) && (error != nil))
{
NSLog(#"Error deleting asset: %#", [error description]);
}
}];
}
}
}
When using the Photos framework, don't forget to link Photos.framework in your target and also import the header in your source file: #import <Photos/Photos.h>
I have a similar problem, and haven't been able to resolve it either. I think its a bug in 8.3 with the AssetsLibrary, and would suggest you submit a bug report to Apple as I have:
Can't edit photo metadata using AssetsLibrary in iOS 8.3 (worked in 8.2)
The user has denied the application access to their media
This explains why you have the error. As far as the system is concerned you don't have access to the photo library.
You need to look at checking the authorization status and requesting if needed as show in the Apple Doc's
You need something like:
if([PHPhotoLibrary authorizationStatus] != PHAuthorizationStatus.PHAuthorizationStatusAuthorized){
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
{
if (status != PHAuthorizationStatus.PHAuthorizationStatusAuthorized)
{
//Fail
}
else
{
//SetImageData
}
}
}
I want to save an image (which is downloaded from server) in iOS device photo album and store that image photo album url in local database. My question is, How do i get that photo album image url after saving the image?
I am able to save the image in photo album using the following ALAsset code: But, I need this url image also to be stored in my local db. So next time, i won't download the same image from server and i can load directly from device photo album.
[self.maAssetsLibrary saveImage:image
toAlbum:#"My-Album"
completion:completion
failure:nil];
Please suggest.
UPDATE:
I tried this, but NOT getting me the photo album image URL after saving the image.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// The completion block to be executed after image taking action process done
void (^completion)(NSURL *, NSError *) = ^(NSURL *assetURL, NSError *error) {
};
[self.maAssetsLibrary saveImage:image toAlbum:#"My-Album" completion:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(#"error");
} else {
NSLog(#"url %#", assetURL);
}
} failure:^(NSError *error) {
}];
});
Due to sandboxing you can't get such a url. As #Lord Zsolt proposes in the comment, you can overcome this by saving images in your application's folder. In this case you might e.g. give them a name that serves as key to identify each image.
EDIT
As #MidhunMP commented, I was wrong on that! There is a way (and I'm happy to know that now) and it comes from this Stack Overflow answer, provided by #CRDave in the comment above.
The main point is to use ALAssetsLibrary's writeImageToSavedPhotosAlbum:orientation:completionBlock: method.
It's always nice to learn.
I'm trying to save a video made in an app to a custom album.
I've tried the solution proposed on Saving Video in an Album Created, however, these blocks are executed asynchronously resulting in my asset in the result block being nil.
I've succeeded in creating the album, writing a video to it doesn't seem to work with the above methods. I have no clue what's going on. Can someone give me a heads up on this?
The url you got is a file url but not an ALAsset url.
You'll need to save that mov to camera roll first and add its asset reference to the custom album.
Check out the example code in this tutorial.
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
for video saving support, just add the function below to the category offered here:
http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/
-(void)saveVideoLocalUrl:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
//add the asset to the custom photo album
[self writeVideoAtPathToSavedPhotosAlbum:assetURL completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"error: %#", [error description]);
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}];
}
I want to save a image captured with AVCaptureStillimageOutput and I'm trying to save it using this code :
[self.library writeImageToSavedPhotosAlbum:image metadata:nil completionBlock:nil]:
it's by default saving to PhotoRoll and there is no option to change album.
I found an older guide o how to save image to album using this code:
[self.library saveImage:img toAlbum:albumName withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
but it seems to be deprecated... Is it possible to make it nondeprecated because i think that this method is the one I'm looking for.
All photos go to the SavedPhotos. Once you saved it there you can use the library method
addAssetsGroupAlbumWithName:resultBlock:failureBlock:
and then the ALAssetsGroup method
addAsset:
Please see this answer for more detail:
Create, Delete, and add pictures to albums in the photos app?