您的位置:首页 > 其它

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

2016-06-30 17:26 399 查看
判断相册中是否存在某张照片
        现在遇到一种极端问题,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]);
}];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  照片是否存在