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];
}
];
Related
In my app I have to implement save image feature. I have managed saving like this:
UIImage *image = [UIImage imageNamed:actualBackground];
UIImageWriteToSavedPhotosAlbum(
image, self,
#selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:),
nil);
/* ... */
- (void)thisImage:(UIImage *)image
hasBeenSavedInPhotoAlbumWithError:(NSError *)error
usingContextInfo:(void *)ctxInfo {
if (!error){
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentViewController:picker animated:YES completion:nil];
}
}
Unfortunately I have to check if file already exists to prevent redundant saving. Also this is necessary because saving multiple times the same image don't override one file, but it creates copy of it...
Do you know how to solve that problem?
SOLUTION:
According to Shravya Boggarapu answer I'm storing assetUrl in my NSUserDefaults. Complete code:
- (IBAction)onDownloadClick:(UIButton *)sender {
UIImage *image = [UIImage imageNamed:actualBackground];
NSString *key = [NSString stringWithFormat:#"assetsUrl %#", actualBackground];
NSString *savedValue =
[[NSUserDefaults standardUserDefaults] stringForKey:key];
NSURL *url = [NSURL URLWithString:savedValue];
if (url != nil) {
PHFetchResult *fetch =
[PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:url]
options:nil];
if ([fetch count]) {
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:nil
message:NSLocalizedString(#"Already downloaded", nil)
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[myAlert show];
return;
}
}
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library
writeImageToSavedPhotosAlbum:image.CGImage
orientation:(ALAssetOrientation)image.imageOrientation
completionBlock:^(NSURL *assetURL, NSError *error) {
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
NSLog(#"assetURL %#", assetURL);
NSString *ass = [assetURL absoluteString];
[[NSUserDefaults standardUserDefaults]
setObject:ass
forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:nil
message:NSLocalizedString(
#"Image downloaded", nil)
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[myAlert show];
}
failureBlock:^(NSError *error){
}];
}];
}
Hope it will help somebody.
I have a solution but not one that applies to every situation.
The thing about saving image to camera roll is that there is an assetURL created when you add the image to camera roll. Also, this asset URL is new every time, so if you save to camera roll, it will create a copy. The name of the image is also not retained.
If the image is added to the camera roll through your app in the first place, then you can store the assetURL as part of image information.
In my app, a dictionary is maintained for every image containing some critical info. This includes the assetURL of the image if it is saved to camera roll.
Once you have the URL, you can check its existence by using fetchAssetsWithALAssetURLs:options: function.
If the URL is nil or the asset when fetched is nil, it means that the image does not exist in the camera roll. So you can add anew.
There is no easy way to compare two images and if you found one that will surly a heavy task, instead of this we can compare image's meta data which we can write it to image and also read it from images.
You can use this repo for that
And ALAssetLibrary also have some properties to access an images meta info. If you google with this you will surly get some.
Like this one
My app takes pictures, saves them to the camera roll, and allows the modification of the EXIF metadata.
I use the AssetsLibrary to save and modify the photo (I can't use the new PhotoKit api because I need to modify the underlying EXIF, plus it's a legacy app and would require a lot of refactoring to change it).
I am using Xcode 6.3.1, with the iOS 8.3 SDK, deployment target of 6.1.
In iOS 8.2, all this worked.
In iOS 8.3, the editing of the metadata fails.
The app has permission in the Privacy settings to access Photos.
When a user modifies the photos, and the app tries to rewrite the photo, iOS 8.3 now shows the "Allow App to modify this photo" dialog. This dialog did not appear in iOS 8.2. If I click "Modify", the photo is saved, but the metadata is wiped. There is also no error returned from setImageData, and I check if the photo is editable.
Here is the code:
-(void)savePhoto:(ALAsset*)asset
{
ALAssetRepresentation* rep = [asset defaultRepresentation];
CGImageRef imageRef = [rep fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if ([asset isEditable])
{
[asset setImageData:imageData metadata:self.getMetadataDictionary completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error!=nil)
[self showErrorDialog:error title:#"Error saving photo" ];
[self closeSaveDialog];
}];
}
else
{
[self closeSaveDialog];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Error saving photo" message:#"Photo is not editable" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
When debugging this, I noticed a bug in my code where the photo size was being increased by 4x by the UIImageJPEGRepresentation, because it was resampling the photo, so I changed the code to grab the original image bytes, and just rewrite the metadata. But interestingly, I get a different error back, this time setImageData returns this error.
Description : "User denied access".
Underlying error : "ALAssetsLibraryErrorDomain" Code -3311.
FailureReason : "The user has denied the application access to their media".
Which is strange, because the app created the asset, and it has acccess to the camera roll.
Again, this code works in iOS 8.2:
-(void)savePhoto:(ALAsset*)asset
{
ALAssetRepresentation* rep = [asset defaultRepresentation];
// New way handling updating photos, doesn't modify image data at all, only metadata
Byte *buffer = (Byte*)malloc((unsigned long)rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil];
NSData *imageData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
if ([asset isEditable])
{
[asset setImageData:imageData metadata:self.getMetadataDictionary completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error!=nil)
[self showErrorDialog:error title:#"Error saving photo" ];
[self closeSaveDialog];
}];
}
else
{
[self closeSaveDialog];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Error saving photo" message:#"Photo is not editable" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
I submitted a bug report to Apple, but havn't heard anything back.
This is similar to this question : setImageData fails in iOS 8.3
Does anyone know of a fix for this?
Thanks,
In apps such as Tweetbot there is a "choose most recent picture" function. I would like to know what the path is for the most recent image in the camera roll.
I know that when I pick an image from camera roll manually, the code to retrieve said image looks like this:
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
What I do not know is what to put in the info valueForKey for the newest picture. I googled extensively and found nada. I know the rules- I am supposed to show what I have tried, but I am at a loss as to what to try, as I am finding zilch in my research and am not familiar with file path related programming.
I read the docs and found nothing of use. Thank you!
In this example, the most recent photo is stored in the UIImage named latestPhoto. Hope this helps!
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
if (alAsset)
{
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
}
}];
}
failureBlock: ^(NSError *error)
{
// an error has occurred
}];
I want to create custom photo album and save photo to specific album in iOS 7. and I found iOS save photo in an app specific album using ALAssetsLibrary.
But I don't know how to do it using UIActivityViewController.
NSArray* actItems = [NSArray arrayWithObjects: image, nil];
UIActivityViewController *activityView = [[UIActivityViewController alloc]
initWithActivityItems:actItems
applicationActivities:nil];
[activityView setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if ([activityType isEqualToString:UIActivityTypeSaveToCameraRoll])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"#Saved_title", nil)
message:NSLocalizedString(#"#Saved_message2", nil)
delegate:self
cancelButtonTitle:NSLocalizedString(#"OK", nil)
otherButtonTitles: nil];
[alert show];
}
}];
I realize the question is a few months old and that the OP may have moved on, but I needed to do exactly this, but didn't find another solution, and needed to come up with my own.
It's not fool-proof, as if there are multiple copies of the video/image in Camera Roll that you're wanting to copy, it will copy all of them, which may or may not be desired. Also, for my purposes, I'm considering maintaining a persistent list of asset URLs that I've written to the custom album so that I skip that asset in the future, but I haven't yet coded it because, if the user removes the video/image from the custom album, it'd be nearly impossible to update that list without more iteration through asset groups: something I'd like to avoid.
Last disclaimer: I haven't tested this a whole ton yet, but it works for my purposes, so hopefully someone else out there can benefit from it.
I augmented Marin Todorov's ALAssetsLibrary+CustomPhotoAlbum category, which I found here: https://github.com/yusenhan/Smooth-Line-View/tree/master/ALAssetsLibrary%2BCustomPhotoAlbum. Note: there is no license information provided there, so I'm assuming that modifying the source for this category is ok.
Here is my method which sets up and displays the UIActivityViewController:
- (void)showActivitySheet:(NSString *)path {
NSURL *newURL = [NSURL fileURLWithPath:path];
NSArray *itemsToShare = #[newURL];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
// Once the OS has finished with whatever the user wants to do with it,
// we'll begin figuring out if we need to save it to the Album, too!
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
// If the user selected "Save to Camera Roll"
if ([activityType isEqualToString:UIActivityTypeSaveToCameraRoll]) {
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
NSURL *tempUrl = [NSURL fileURLWithPath:path];
// saveMovie: below is a method I added to the category, but you can use saveImage:,
// which you'll also likely want to add some kind of parameter to so, in the category,
// you know when you only want to copy to the Album instead of the Album AND Camera Roll
[lib saveMovie:tempUrl toAlbum:#"CUSTOM ALBUM NAME"
withCompletionBlock:^(NSURL *url, NSError *error) {
if (error) {
NSLog(#"Error writing movie to custom album: %#", error.debugDescription);
}
}
onlyWriteToCustomAlbum:YES];
}
}];
[self presentViewController:activityVC animated:YES completion:nil];
}
Here is code you can add to the saveImage: method in the ALAssetsLibrary+CustomPhotoAlbum category so you can save it to your custom Album, too! You could execute this portion only on the YES case of the BOOL which you'll want to add to the category.
NSData *dataToCompareTo = [NSData dataWithContentsOfURL:url]; // This data represents the image/movie which you want to save into the custom album. The one you handed to the UIActivityViewController.
// Note use of self, as this is a category on ALAssetsLibrary; also, this
// assumes that you've already written the photo to the Camera Roll, which
// should be automatically handled by the OS, if the user hit the "Save" button
// on the UIActivityViewController.
// Enumerate through Camera Roll/Saved Photos group.
[self enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Enumerate the assets in each group.
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
ALAssetRepresentation *rep = [result defaultRepresentation];
// If the asset isn't the same size as the source image/movie
// it shouldn't be the same. Check the size first, since it
// is a less costly operation than byte checking the data itself.
if ([rep size] == [dataToCompareTo length]) {
Byte *buffer = malloc([NSNumber numberWithLongLong:rep.size].unsignedLongValue);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:[NSNumber numberWithLongLong:rep.size].unsignedLongValue error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
// If the buffer has more than the null-termination char, free it! I'm doing this in case the above call to dataWithBytesNoCopy fails to free() the buffer.
if (sizeof(buffer) > 4) {
free(buffer);
}
// Ensure they are the same by comparing the NSData instances.
if ([data isEqualToData:dataToCompareTo]) {
NSLog(#"they are the same!!");
[self addAssetURL:[rep url]
toAlbum:albumName
withMovieCompletionBlock:completionBlock];
} else {
NSLog(#"they are different");
}
}
}];
} failureBlock:^(NSError *error) {
NSLog(#"Failed to write to custom album!");
}];
Hope it helps! TLDR? Thought so! ;-)
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".