I am creating an app that fetches photos and videos from the library.
I am using ALAssetsLibrary to fetch the contents from the library. But I can't retrieve the videos album from the library using this.
I am using the following code to retrieve the contents:
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group || group.numberOfAssets >= 1)
{
[tmpAssets addObject:group];
}else{
self.assetAlbums = tmpAssets;
*stop = YES;
if(self.assetAlbums.count){
//parse contents and reload view
}
}
}
failureBlock:^(NSError *error)
{
//failed
}];
I am getting all other pictures and videos.
I have used a workaround of creating one videos album and filling its contents with the results from the ALAssetsLibraryGroupsEnumerationResultsBlock with allVideos filter.
but the ones that I upload from iTunes (those that get into the videos folder in the library) are not getting retrieved.
Related
There are many examples to import one or many images from gallery using UIImagePicker, however I want to detect new images automatically (just like dropbox) and import new images inside my app. Is there any way I can detect new images without opening imagepicker? if not what are the possibilities? Thank.
You can easily get all images information without UIImagePicker by using ALAssetLibrary it will gives you all images and video information, you can store it locally in coredata and perodically check it if there is an image/video info from ALAssetLibrary which is not in you coredata hanse it’s a new image/video.
ALAssetLibrary example code from my personal app and it works prefectoly
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
if (alAsset)
{
ALAssetRepresentation *representation =[alAsset defaultRepresentation];
NSURL *url = [representation url];
NSString *assetType=[alAsset valueForProperty:ALAssetPropertyType];
// You can store this info in coreData
}
}];
} failureBlock: ^(NSError *error)
{
UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:#"Permission Denied" message:#"Apple restrictions prevent our app from accessing your Library without your permission. In order to access your media you must enable privacy setting " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alrt show];
}
];
This code can't find photos from my Macbook pro (meta is nil). Trying to test this code on my MBP. The photos do exist in iPhoto. Is this code only works for iPhone/iPad? If yes, how do I test this code on my MBP?
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){
if (asset){
NSDictionary *meta = [[asset defaultRepresentation] metadata];
}
}];
}
} failureBlock:^(NSError *error) {
NSLog(#"error enumerating AssetLibrary groups %#\n", error);
}];
ALAssetsLibrary is only available for iOS. I assume you're running an iOS app in the simulator, is that right? iOS apps running in the simulator or a device can't access native Mac resources, due to the app sandboxing, and other similar reasons.
I need to know if an image is already in the iOS gallery. I am developing a messaging client app and, in my chat list, I am able to download images/videos of incoming messages when I tap into the image thumbnail.
The issue is, when I tap in the thumbnail, I download and save that image into the iOS photo gallery, but if I tap again I don't want to download and save it again, I want to retrieve it from the photo gallery.
Resuming, I want to look for the image in the photo gallery and retrieve it.
Here is my code to save the image in my custom photo album using ALAssetsLibrary:
[self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:
^(NSURL* assetURL, NSError* error)
{
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}
];
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:
^(ALAssetsGroup *group, BOOL *stop)
{
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
albumWasFound = YES;
[self assetForURL: assetURL
resultBlock:
^(ALAsset *asset)
{
//add photo to the target album
[group addAsset: asset];
completionBlock(nil);
} failureBlock: completionBlock
];
return;
}
if (group==nil && albumWasFound==NO) {
//Target album does not exist, create it
__weak ALAssetsLibrary* weakSelf = self;
[self addAssetsGroupAlbumWithName:albumName
resultBlock:
^(ALAssetsGroup *group)
{
[weakSelf assetForURL: assetURL
resultBlock:
^(ALAsset *asset)
{
//add photo to the newly created album
[group addAsset: asset];
completionBlock(nil);
} failureBlock: completionBlock
];
} failureBlock: completionBlock
];
return;
}
} failureBlock: completionBlock
];
}
Maybe with the assetURL I could do it, but I have been searching in Apple's documentation and I didn't see anything.
Thanks!!
You can get Asset URL after the save process using completion block. You can retrieve the saved image using this Asset URL.
Unfortunately, you can't check file exist in Photolibrary. The reason behind is, the system will automatically embed many extra information with the image while you save in to Photolibrary. example like location, time etc.. So the binary content will be different for same images.
So, you have only one solution. Save the images into document directory and do a file exist using algorithms like CRC, MD5 etc..
I am making a camera app where I want to get to all of the videos users have created on their iPhone.
Currently the code I have gets the videos from user Camera Roll only. Some my users have complained that they have multiple custom folders created under their Photo Album app and they store some videos in there. Since my code only looks at Camera Roll, it doesn't pickup the movies from their other folders. Is it possible that I can get to their other folders?
This is what I have so far.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
if (alAsset)
{
ALAssetRepresentation *representation =[alAsset defaultRepresentation];
NSURL *url = [representation url];
NSString *assetType=[alAsset valueForProperty:ALAssetPropertyType];
//videos only
if ([assetType isEqualToString:#"ALAssetTypeVideo"])
{
.....
You got to create a filter for the assets, something like this:
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
ALAssetsFilter *allVideosFilter = [ALAssetsFilter allVideos];
[group setAssetsFilter:allVideosFilter];
//...
};
Options for filters are:
- allAssets
- allVideos
- allPhotos
Hope this helps
To get media that was synced from iTunes you need to use ALAssetsGroupLibrary. Here you can find all possible variants for ALAssetsGroupType. So just change
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:...
to
[library enumerateGroupsWithTypes:(ALAssetsGroupSavedPhotos | ALAssetsGroupLibrary) usingBlock:...
This retrieves all videos, including any the user has synced from iTunes:
// Enumerate just the photos and videos by using ALAssetsGroupSavedPhotos
[library enumerateGroupsWithTypes:ALAssetsGroupAll | ALAssetsGroupLibrary
usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group != nil)
{
// Within the group enumeration block, filter to enumerate just videos.
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
// Do whatever you need to with `result`
}
}];
} else {
// If group is nil, we're done enumerating
// e.g. if you're using a UICollectionView reload it here
[collectionView reloadData];
}
} failureBlock:^(NSError *error) {
// If the user denied the authorization request
NSLog(#"Authorization declined");
}];
Note the ALAssetsGroupAll | ALAssetsGroupLibrary.
According to the docs ALAssetsGroupAll is "the same as ORing together all the group types except for ALAssetsGroupLibrary". So we also add ALAssetsGroupLibrary which "includes all assets that are synced from iTunes".
I am able to save an image into a custom album in the iPhone camera roll. For example, the album name could be "Fun", and all of the images I take will go into that folder. I came across a helper class here that works very well to accomplish what I want.
However, the method I am using does not save videos into the custom folder, but the folder is created - it's just empty of any videos that I attempt to save there. I tried debugging it with no progress. That method is here:
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;
//search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//compare the names of the albums
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
//target album is found
albumWasFound = YES;
NSData *data = [NSData dataWithContentsOfURL:assetURL];
if ([data length] > 0) {
//get a hold of the photo's asset instance
[self assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
[group addAsset: asset];
//run the completion block
completionBlock(nil);
} failureBlock: completionBlock];
}
//album was found, bail out of the method
return;
}
if (group==nil && albumWasFound==NO) {
//photo albums are over, target album does not exist, thus create it
__weak ALAssetsLibrary* weakSelf = self;
//create new assets album
[self addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *group) {
//get the photo's instance
[weakSelf assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
NSLog(#"asset: %#",asset);
//add photo to the newly created album
[group addAsset: asset];
//call the completion block
completionBlock(nil);
} failureBlock: completionBlock];
} failureBlock: completionBlock];
//should be the last iteration anyway, but just in case
return;
}
} failureBlock: completionBlock];
}
Does anyone know the proper way to save a video into a custom album in the photo library?
Try this, it works for me.
-(void)saveVideo:(NSURL *)videoUrl toAlbum:(NSString*)albumName withCompletionBlock: (SaveImageCompletion)completionBlock
{
//write the image data to the assets library (camera roll)
[self writeVideoAtPathToSavedPhotosAlbum:videoUrl completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}];
}