Harshal Chaudhari
Harshal Chaudhari

Reputation: 710

How to know image present at the url

I have url for image picked using ELCImagePickerController. I stored the url for future reference.

I get that URL using:

[dict valueForKey:UIImagePickerControllerReferenceURL];

Now the problem arises when after some time user deleted that particular image from photo library and I am going to access that image using URL. My app does not get crashed.

I have tried using NSUrl method

[imagePath checkResourceIsReachableAndReturnError:&err]

as well i tried something like:

-(BOOL)findImage:(NSURL*)path
{
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    __block BOOL flag=YES;
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullScreenImage];
        if (iref) {
            flag=YES;
            dispatch_group_leave(group);
        }
    };
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"cant get image - %@",[myerror localizedDescription]);
        flag=NO;
    };
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:path resultBlock:resultblock failureBlock:failureblock];

    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_release(group);
    [assetslibrary release];

    return flag;
}

Sample url :

assets-library://asset/asset.JPG?id=E862927E-E646-448A-9EB6-A7D48668B3DC&ext=JPG

But no success.

How to know that image present at particular URL.

If any one can help me out on this will be appreciable.

Thanks in advance.

Upvotes: 0

Views: 323

Answers (2)

Harshal Chaudhari
Harshal Chaudhari

Reputation: 710

Solved the problem with change in the findImage method

-(BOOL)findImage:(NSURL*)path
{
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    __block BOOL flag=YES;
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:path resultBlock:^(ALAsset *asset) {
        if (asset==nil)
        {
            flag=NO;
        }
        else
        {
            flag=YES;
        }
            dispatch_group_leave(group);
        } failureBlock:^(NSError *error){
                NSLog(@"operation was not successfull!");
                dispatch_group_leave(group);
    }];
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_release(group);
    [assetslibrary release];
    return flag;
}

Upvotes: 1

Jim
Jim

Reputation: 4719

For this case you need to check ALAssetRepresentation *rep = [myasset defaultRepresentation] to nil.

if(rep != nil){ //write your code.. }

Upvotes: 1

Related Questions