iOS - Cached data used by ALAssetsLibrary is not updated - iphone

I seem to be having caching issues with the ALAssetLibrary when testing my app on the iPhone.
Steps used to produce this issue
Take a photo using the iPhone camera
Access the Camera Roll using ALAssetsLibrary (through ELCImagePicker)
The problem is that when ELCImagePicker displays the list of albums, the thumbnail of the photo taken in step 1 is displayed for the Camera Roll album (which is correct), but when I go into the Camera Roll album, that photo is not there. An error like this is also displayed in the console log:
Cached count is off for 0x2391d0 <x-coredata://D226A7C3-95D5-40B3-BCFB-726E534AB57A/Album/p1> (1357 != 1358) moc=<PLManagedObjectContext: 0x63794c0>
1357 and 1358 appears to be match the photo count before and after I take the photo, and closing the image picker and opening it again doesn't help. But if I switch to the Photos app, the photo is displayed there. And when I return to my app, the photo is now displayed as well.
I have tried both
UIImageWriteToSavedPhotosAlbum ( UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo );
and ALAssetsLibrary's
(void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock
but neither works. Any help is greatly appreciated. Thanks!

You can listen for ALAssetsLibraryChangedNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reloadAlbumGroups) name:ALAssetsLibraryChangedNotification object:nil];
In reloadAlubmGroups load your album groups from assets library to get updated objects.
Apple Documentation:
ALAssetsLibraryChangedNotification
Sent when the contents of the assets library have changed from under the app that is using the data.
When you receive this notification, you should discard any cached information and query the assets library again. You should consider invalid any ALAsset, ALAssetsGroup, or ALAssetRepresentation objects you are referencing after finishing processing the notification.
In iOS v4.0, the notification’s object is nil. In iOS v4.1 and later, the notification object is the library object that posted the notification.

Related

Can i save photos to the camera roll without user explicit consent?

i'm developing an iPhone application that let users to take photos. Those photos are saved inside the application, but i would like also to save them to the camera roll.
The problem is that my application does not have a UI control that let the user specify if he wants the photo to be saved also in the camera roll.
I searched the Apple guidelines and documentation but could not find any hint regarding this issue.
So in your experience is ok to save photos taken by the user also in the camera roll without asking his permission?
It is perfectly fine to write to camera roll without permission..but it is better to show a successful save alert once you save photo.. user won't be happy if you are saving unnecessary/involuntary photo without some feedback to user
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
above is the method to save the image .. it can be find in apple docs here Save to camera roll
If the user is taking the photo himself by tapping a button or whatever, saving to the camera roll is the best you could do, IMO, and I don't think you need to show an alert or whichever kind of notice.
The only thing, make sure the user knows where he should go to retrieve his pictures...

How to save image in my device image library folder from my app document folder in iPhone?

I am receiving some image from web-service and store them into my app document folder. Now I have create an image library with my app name in iPhone device. Now. I want to show the images in this image library.
For saving the system image I am using this code -
[self.library1 addAssetURL:url toAlbum:#"My_App_Name" withCompletionBlock:^(NSError *error) {
if (error!=nil) {
NSLog(#"Big error: %#", [error description]);
}
}];
but this is not work when I get the url of my document folder image. Help me out or gave any solution.
iOS5 includes new API in the AssetsLibrary framework, which afford you the chance to manage your app’s own photo album inside the user’s photo library.
I think it is easy peasy. Your app should create an UIImage and will just need to pass it to the below method along with the album name and a block to handle.
-(void)saveImage:(UIImage*)image
toAlbum:(NSString*)albumName
withCompletionBlock:(SaveImageCompletion)completionBlock;
You can see more details here

Camera Roll asset images not available after saving an image to Saved Photos

I am using ALAssetsLibrary to make an array of all images in the camera roll when my iPhone app starts. I store every image as a ALAsset * in an array.
Later in the app, I am saving an image to the gallery using this:
UIImageWriteToSavedPhotosAlbum(croppedImage, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
The image is saved successfully to the saved photos album. But, now my problem is that when I try to retrieve images using the existing array of all images, I don't get anything. Do I need to build the array of all images again or is there some other way to do this?
Saving an image changes the library and invalidates objects like assets from the library. You need to reload assets from the library whenever the library changes - do that when you receive an ALAssetsLibraryChangedNotification.

Create, Delete, and add pictures to albums in the photos app?

Haven't seen anything, but I have seen that you can create albums in the iOS 5 photos.app, and I was curious if there is a way to integrate it in my app.
I need to be able to create the album, upload pictures to it, and be able to delete it.
Thanks,
Coulton
Since iOS5 you can programmatically add albums to Photos. See the ALAssetsLibrary Class Reference.
You can add an album (which is an ALAssetsGroup) with:
addAssetsGroupAlbumWithName:resultBlock:failureBlock:
Every ALAssetsGroup has a URL associated with it. You'll want to save it:
valueForProperty:ALAssetsGroupPropertyURL
You can then use that URL to get an ALAssetsGroup object:
groupForURL:resultBlock:failureBlock:
Once you have your ALAssetsGroup you can use addAsset: method on it to add an image or video.
Notice: all these methods are asynchronous and return immediately. You do the actual work in the blocks you pass to these methods.
See the UIImage class reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum
Adds to the camera roll album (camera) or saved photos album (no camera).

Retrieving an iPhone photo saved to the photo library

I am saving images to the photo library and would like to retrieve them dynamically to display in future launches of my app. I use the WriteToSavedPhotosAlbum function, as below, but do not get any info to access the saved photo programmatically.
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
Once saved, is there a way to retrieve the file name/id and load an image from the photo library at a later time?
You just need to save them for yourself.
Possibly in your app's documents or cache folder.
As a side note, when you say: "display at launch", if you mean to replace the Default.png, that won't work.
The launch image must be static and included in your app bundle.
If not, disregard.
You can't, unless the user picks the photo using the UIImagePickerController.
Check this related question
try this code segment make the necessay changes
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=pickerDelegate;
picker.sourcetype=UIImagepickerControllerSourceTypePhtoLibrary;