判断相册中是否存在某张照片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_30657013/article/details/51791171

判断相册中是否存在某张照片

        现在遇到一种极端问题,App中有一个功能是选择照片,允许多选,多选后,回到桌面,打开系统的相册将选择的照片删除,然后再返回App点击使用,这时候由于图片被删除会发生异常闪退.下面的方法可以在使用之前判断图片是否存在

// photoUrl 是相册图片的链接 ALAssetsLibrary 最好是写一个单例 确保每个地方取到的是同一个
//ALAsset 是照片对象
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
 NSString *url=[[ALAsset valueForProperty:ALAssetPropertyAssetURL] absoluteString];
    [assetLibrary assetForURL:photoUrl resultBlock:^(ALAsset *asset) // substitute YOURURL with your url of video
     {
         ALAssetRepresentation *rep = [asset defaultRepresentation];
         Byte *buffer = (Byte*)malloc(rep.size);
         NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
         NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
         NSLog(@“photo data length  %d",data.length);
         if (data.length == 0)
         {
         NSLog(@"照片不存在");
         }
     }
                 failureBlock:^(NSError *err) {
                     NSLog(@"Error: %@",[err localizedDescription]);
                 }];

猜你喜欢

转载自blog.csdn.net/sinat_30657013/article/details/51791171