Different behavior between iPhone and iPad with the Assets Library - iphone

I'm using the Assets Library on an app to enumerate the Photos Events of a device.
My code works fine when i test it on my iPad. The Photos Events are enumerated and i can handle them perfectly. When I try the very same code on my iPhone, nothing happens (and I have Photos Events on this device too). It looks as if the enumeration code was not even called (ie no log appears in the console, cf. code).
Here is the code :
- (void)loadEvents {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupEvent
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[photosEventsArray addObject:group];
NSLog(#"Adding group");
} else {
NSLog(#"End of the enumeration");
}
}
failureBlock: ^(NSError *error) {
NSLog(#"Failure while enumerating assets: %#", error);
}];
[library release];
NSLog(#"Found %d events", photosEventsFound);
[self performSelectorOnMainThread:#selector(stopSpinner) withObject:nil waitUntilDone:YES];
[pool drain];
}
My deployment target is iOS 4.1.
Any idea of what is going wrong here?

After more investigation, it seems that on iOS 4.3.5, the enumerateGroupsWithTypes method HAS to be called from the main thread.
I've patched my code this way (setting NO from iPhone and iPod Touch, and YES from iPad):
if (scanAssetsInBackground) {
[self performSelectorInBackground:#selector(loadEvents) withObject:nil];
} else {
[self performSelectorOnMainThread:#selector(loadEvents) withObject:nil waitUntilDone:YES];
}
Works fine with that patch.
There's not much information about this in Apple docs and there's no way to know which way (background or main thread) is the right way to scan assets libraries.

Related

iPhone App Crash with error [UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:]

I have an iPhone App in the app store which is using Touch ID. If Touch ID is enabled, the user is authenticated with it, else user needs to enter his PIN to login to the application.
After IOS 10.1 release, when I checked the crash report, the crash count has increased. From the crash report, it is pointing on [UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:] and when I opened the app in Xcode, it is focussing on [self dismissViewControllerAnimated:YES completion:nil];.
The code I have written is as below:
-(void) showTouchIDAuthentication{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = #"Authenticate using your finger to access My Account Menu.";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"User is authenticated successfully");
[self dismissViewControllerAnimated:YES completion:nil];
} else {
}];
}
}
When I tested in iPhone 6, IOS 10, everything is working fine. Don't know how to simulate the issue.
Can anyone please figure out if I am missing something? Please help me out in resolving this crash issue.
Usually completion handlers are not running on main thread. All UI related stuff must be done on main thread (including dismissing a view controller).
I suggest to add the dismiss line on a main thread block like this:
-(void) showTouchIDAuthentication{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = #"Authenticate using your finger to access My Account Menu.";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"User is authenticated successfully");
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[self dismissViewControllerAnimated:YES completion:nil];
}];
} else {
}];
}
}

Zeroing ALAssetRepresentation after saving image captured via UIImagePickerController in iOS5

In my app (which worked under iOS 4) I collect pictures selected via UIImagePickerController. Unfortunately, I have a strange problem after upgrading to iOS 5.
In a nutshell, I store ALAssetRepresentation in NSMutableArray. When I add photos from Library, everything is ok. However, when I capture and save a picture, all ALAssetRepresentations (including a new one) become 0-sized. ALAssetRepresentation.size and ALAssetRepresentation.getBytes:fromOffset:length:error: return 0 and getBytes:error is nil.
I init ALAssetsLibrary in AppDelegate, so the
“The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance.” condition is OK.
Is there a way to prevent ALAssetRepresentation from zeroing? Or how can I read image by bytes after this?
My code:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary){
[self addPhoto:[info valueForKey:UIImagePickerControllerReferenceURL]];
}
else if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera){
[self savePhoto:[info valueForKey:UIImagePickerControllerOriginalImage]];
}
[self dismissModalViewControllerAnimated:YES];
}
-(ALAssetsLibrary*) getLibrary{
if (!library){
testAppDelegate *appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
library = appDelegate.library;
}
NSLog(#"getLibrary: %#", library);
return library;
}
-(void) addPhoto:(NSURL*) url{
ALAssetsLibraryAssetForURLResultBlock successBlock = ^(ALAsset *asset_){
ALAssetRepresentation *assetRepresentation = [[asset_ defaultRepresentation] retain];
[photos addObject: assetRepresentation];
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){
NSLog(#"Error: Cannot get image. %#", [error localizedDescription]);
};
[[self getLibrary] assetForURL:url resultBlock:successBlock failureBlock:failureBlock];
}
- (void)savePhoto:(UIImage *)image {
[[self getLibrary] writeImageToSavedPhotosAlbum:[image CGImage] orientation:[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(#"Error: Cannot save image. %#", [error localizedDescription]);
} else {
NSLog(#"photo saved");
[self addPhoto:assetURL];
}
}];
}
I solved it!
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.
Have you tried retaining the ALAssets instead of the ALAssetRepresentation? This should work, I have used this approach before.

Iphone: unable to show photos using AlAssetsLibrary

I currently sending ipa to friends for testing. Funny thing is, one of my tester able view her photos stored on her phone which was running IOS 5 using iPhone 4.
Another 2 testers: one has iPhone 4 (IOS 4.3.3) , and iPhone 3GS (IOS 5.0.1) both of them can't see photos stored on their phone.
These are the code I have used:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
//NSLog(#"See Asset: %#", #"ggg");
[assets addObject:result];
}
};
NSLog(#"location = %i length = %i ", range->location, range->length );
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop) {
if(group != nil) {
NSRange *datarange = malloc(sizeof(NSRange));
range->total = [group numberOfAssets];
datarange->location = [group numberOfAssets] - range->location - range->length;
datarange->length = range->length;
NSLog(#" total = %i", range->total);
int location = [group numberOfAssets] - range->location - range->length;
if (location < 0)
{
datarange->location = 0;
datarange->length = [group numberOfAssets] - range->location;
}
NSIndexSet *indexset = [ [NSIndexSet alloc] initWithIndexesInRange:*datarange];
[group enumerateAssetsAtIndexes:indexset options:NULL
usingBlock:assetEnumerator];
[indexset release];
free(datarange);
[self loadAssetToScrollView:assets];
}
};
[assets release];
assets = [[NSMutableArray alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) {
NSLog(#"Failure");
}];
[library release];
I saw somebody say about asynchronous thing in some other threads but don't know is it the case. He say put dispatch_async in the enumerate group block.
Does anyone know what is wrong.
Additionally, one of tester with iOS 4.3.3 can show his photos after enabling location services under General->Setting. Why we have to enable it? Can we enabled it on code since it will be quite disturbing to the user who using our application.
Also on iOS 5.x you must retain your ALAssetsLibrary instance so long as you need to work with the collected assets. When you release your ALAssetsLibrary instance like in your code just after calling [library enumerateGroupsWithTypes:…] all the collected assets will be invalid.
See also the ALAssetsLibrary doc - overview:
"… The lifetimes of objects you get back from a library instance are tied to the lifetime of the library instance. …"
Yes, it is incredibly frustrating, but that is how it is, and you cannot enable location services in code (that is a good thing though).
I would move the first block ^assetGroupEnumerator to the heap by [[<#block#> copy] autorelease]. Why? Because this block would be autoreleased by the runloop, if there are many assets need to be enumerated through.
One more thing: don't use [self loadAssetToScrollView:assets]; inside the block but get the weak reference of self before the block like this:
__block YourExampleClassInstance *weakSelf = self;
and further use this weakSelf instance inside the block:
[weakSelf loadAssetToScrollView:assets];
void (^assetGroupEnumerator)… = ^(ALAssetsGroup *group, BOOL *stop) {
…
};
Why? To avoid retain cycles.

ELCImagePickerController without allowing access to user location

I used ELCImagePickerController in my app.
but when we start our application first time and on Image gallery it will ask for accessing user location , if we don't allow to access user location to it then it will give error in UIAlertView and it wont show image gallery.
But after that if we go to setting app -> location services -> [turn on switch for access location for our app] then start app -> go to gallery page -> we can show image gallery in our app.
So my question is how can we show image gallery with ELCImagePickerController with location services turn off for our app or user don't allow to access location to our app.
ELCImagePickerController can be downloadable at this LINK
Then find ELCAlbumPickerController.m file then go to View Did Load then this causes error alert when user location access turned off,
dispatch_async(dispatch_get_main_queue(), ^
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Group enumerator Block
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if (group == nil)
{
return;
}
[self.assetGroups addObject:group];
// Keep this line! w/o it the asset count is broken for some reason. Makes no sense
NSLog(#"count: %d", [group numberOfAssets]);
// Reload albums
[self performSelectorOnMainThread:#selector(reloadTableView) withObject:nil waitUntilDone:YES];
};
// Group Enumerator Failure Block
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[NSString stringWithFormat:#"Album Error: %#", [error description]] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(#"A problem occured %#", [error description]);
};
// Enumerate Albums
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:assetGroupEnumerator
failureBlock:assetGroupEnumberatorFailure];
[library release];
[pool release];
});
The answer is that you can't show the image gallery with ELCImagePickerController with location services turned off.
ELCImagePickerController uses the Assets Library Framework to access the device's photo album. Because this framework also gives access to the metadata of the photos - including location data - the user needs to grant permission for the app to use Location Services.
There's no way around this, except if you use the standard UIImagePickerController (but I assume that won't cover the requirements for your app)

Memory leaks when using ALAssetsLibrary

I'm developing an iPhone app using SDK 4.1 targeting iOS 4.1 or newer.
Instruments report memory leaks for the code below.
void (^resultBlock)(ALAsset *) = ^(ALAsset *asset) {
NSLog(#"resultBlock");
};
void (^failureBlock)(NSError *) = ^(NSError *error) {
NSLog(#"error");
};
NSURL *url = [NSURL URLWithString:#"assets-library://asset/asset.JPG?id=1000000176&ext=JPG"];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:url resultBlock:resultBlock failureBlock:failureBlock];
[library release];
I just put this code in viewDidLoad of my TestApp for test.(I'm using with UIImagePicker in my actual project.)
When I run the TestApp using Instrument(leak), it report memory leaks about 10 seconds after launch.
Can anybody tell me what's wrong with this code or is there something else that I should do?
Thanks.
There’s nothing wrong with your memory management in the code you’ve provided. If there is a leak, it’s in Apple’s frameworks or another part of your code. Just make sure you’re testing on the device—some frameworks have much more “polish” on the device.