You can do this with Photos Framework which is announced iOS8 release.
Read Documentation about change observer Link
Apple sample sample
Change observing. Use the shared PHPhotoLibrary object to register a change handler for the assets and collections you fetch. Photos tells your app whenever another app or device changes the content or metadata of an asset or the list of assets in a collection.
PHChange
objects provide information about object state before and after each change with semantics that make it easy to update a collection view or similar interface
You can take photos if any update on photos app, by using **photos framework
First you need to register photos observer
- (void)applicationWillTerminate:(UIApplication *)application {
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
[[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
// dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
// //Background Thread
// isBackgroundOrForeground = YES;
// [self getGreenEvent];
// dispatch_async(dispatch_get_main_queue(), ^(void){
// //Run UI Updates
// });
// });
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"=== DID ENTER BACKGROUND ===");
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
}];
if (bgTask == UIBackgroundTaskInvalid) {
NSLog(@"This application does not support background mode");
} else {
//if application supports background mode, we'll see this log.
NSLog(@"Application will continue to run in background");
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
}
}];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
isBackgroundOrForeground = NO;
NSString *oAuthToken = [[NSUserDefaults standardUserDefaults]stringForKey:KEY_ACCESS_TOKEN];
if(oAuthToken.length!=0 || oAuthToken!=nil)
[self getGreenEvent];
}
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
pragma mark - PHPhotoLibraryChangeObserver
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
NSLog(@"content being changed");
/*
Change notifications may be made on a background queue. Re-dispatch to the
main queue before acting on the change as we'll be updating the UI.
*/
__block BOOL reloadRequired = NO;
__block NSIndexSet *removedIndex;
__block NSIndexSet *insertedIndexes;
dispatch_async(dispatch_get_main_queue(), ^{
// Loop through the section fetch results, replacing any fetch results that have been updated.
NSMutableArray *updatedSectionFetchResults = [self.sectionFetchResults mutableCopy];
// __block BOOL reloadRequired = NO;
[self.sectionFetchResults enumerateObjectsUsingBlock:^(PHFetchResult *collectionsFetchResult, NSUInteger index, BOOL *stop) {
PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:collectionsFetchResult];
removedIndex = changeDetails.removedIndexes;
insertedIndexes = changeDetails.insertedIndexes;
if (changeDetails != nil) {
[updatedSectionFetchResults replaceObjectAtIndex:index withObject:[changeDetails fetchResultAfterChanges]];
reloadRequired = YES;
self.sectionFetchResults = updatedSectionFetchResults;
if(insertedIndexes != nil){
[self backgroundUpload];
}else{
}
}
}];
if (reloadRequired) {
self.sectionFetchResults = updatedSectionFetchResults;
}
});
}
-(void)getAllPhotosFromCamera{
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
// Store the PHFetchResult objects and localized titles for each section.
self.sectionFetchResults = @[allPhotos, smartAlbums, topLevelUserCollections];
}
-(NSMutableArray *)getNumberOfPhotoFromCameraRoll:(NSArray *)array{
PHFetchResult *fetchResult = array[1];
int index = 0;
unsigned long pictures = 0;
for(int i = 0; i < fetchResult.count; i++){
unsigned long temp = 0;
temp = [PHAsset fetchAssetsInAssetCollection:fetchResult[i] options:nil].count;
if(temp > pictures ){
pictures = temp;
index = i;
}
}
PHCollection *collection = fetchResult[index];
if (![collection isKindOfClass:[PHAssetCollection class]]) {
// return;
}
// Configure the AAPLAssetGridViewController with the asset collection.
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
self. assetsFetchResults = assetsFetchResult;
self. assetCollection = assetCollection;
self.numberOfPhotoArray = [NSMutableArray array];
for (int i = 0; i<[assetsFetchResult count]; i++) {
PHAsset *asset = assetsFetchResult[i];
[self.numberOfPhotoArray addObject:asset];
}
NSLog(@"%lu",(unsigned long)[self.numberOfPhotoArray count]);
return self.numberOfPhotoArray;
}
Where bgTask is global object of
UIBackgroundTaskIdentifier bgTask;
@property (nonatomic, strong) NSArray *sectionFetchResults;
@property (nonatomic, strong) PHFetchResult *assetsFetchResults;
@property (nonatomic, strong) PHAssetCollection *assetCollection;
@property (nonatomic, strong) NSMutableArray *numberOfPhotoArray;