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");
}];
}}
Related
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));
}];
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 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 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 have an image and i want to save that image into custom photo album.
My code is working fine for Iphone but it is not workin for iPad.
self.library = [[ALAssetsLibrary alloc] init];
[self.library saveImage:imageShow.image toAlbum:#"APP NAME" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
just help me please
Thanx for help..