浏览本地相册和照相并保存本地

这里用到的不是很复杂的东西都是网上现成的东西,我只是做了一次搬运工。
一,拍照后储存照片到相册中
拍完照后将图片储存到相册中这是一个比较简单代码块,这里不说了下面就是代码。

#pragma mark 将照片插入到相册中
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    if ([self isCanUsePhotos] == NO){
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法使用照片" message:@"请在iPhone的""设置-隐私-照片""中允许访问照片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
        [alert show];
    }else{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (image == nil)
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [_selectedPhotos addObject:image];

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        //如果是图片
        UIImage *images = [info objectForKey:UIImagePickerControllerEditedImage];

        UIImageWriteToSavedPhotosAlbum(images, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    [_collectionView reloadData];
    [self imageArray];
    }

}

//是否有授权
- (BOOL)isCanUsePhotos
{
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
        if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied) {
            //无权限
            return NO;
        }
    }
    else {
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusRestricted ||
            status == PHAuthorizationStatusDenied) {
            //无权限
            return NO;
        }
    }
    return YES;
}

二,告述浏览图片器拍到的照片
这是因为浏览相册模块用的是PHAsset格式,所以我们这里也要使用PHAsset格式放入浏览模块中。
我们要获取相册的代码只能获取所有图片,在这里我就要适当的做一下处理。
图片储存也如同我的数据一样它是有一定的规律的,这里我用到了时间作为排序,时间越晚的越在最后。按照这个规律我们只要取到最后一个照片,那就是我刚才拍到的那张照片。下面就是具体的代码

-(NSMutableArray*)GetALLphotosUsingPohotKit
{
    NSMutableArray *arr = [NSMutableArray array];
    // 所有智能相册
    PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    for (NSInteger i = 0; i < smartAlbums.count; i++) {
        // 是否按创建时间排序
        PHFetchOptions *option = [[PHFetchOptions alloc] init];
        option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
        option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
        PHCollection *collection = smartAlbums[i];
//        PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
//        NSLog(@"name:%@",cameraRoll.localizedTitle);
//        NSLog(@"这里有没有不是collection:%@",collection);
        //遍历获取相册
        if ([collection isKindOfClass:[PHAssetCollection class]]) {
//            NSLog(@"相机胶卷:%@",collection.localizedTitle);
            if ([collection.localizedTitle isEqualToString:@"All Photos"] || [collection.localizedTitle isEqualToString:@"Camera Roll"] || [collection.localizedTitle isEqualToString:@"相机胶卷"] || [collection.localizedTitle isEqualToString:@"所有照片"]) {
                PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
                PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
//                NSLog(@"这里有没有不是:%@",fetchResult);
                NSArray *assets;
                if (fetchResult.count > 0) {
                    // 某个相册里面的所有PHAsset对象
                    assets = [self getAllPhotosAssetInAblumCollection:assetCollection ascending:YES ];
//                    NSLog(@"这里有没有:%@",assets);
                    [arr addObjectsFromArray:assets];
                }
            }
        }
    }
    //返回相机胶卷内的所有照片数组
    return arr;
}

- (NSArray *)getAllPhotosAssetInAblumCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
    // 存放所有图片对象
    NSMutableArray *assets = [NSMutableArray array];
    // 是否按创建时间排序
    PHFetchOptions *option = [[PHFetchOptions alloc] init];
    option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
    option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
    // 获取所有图片对象
    PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
    // 遍历
    [result enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {
        [assets addObject:asset];
    }];
    return assets;
}

//图片储存成功后代码,以及告述浏览相册我刚才拍的是那张相片
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSMutableArray *assets = [self GetALLphotosUsingPohotKit];
    //获取最后一张图片
    PHAsset *asse = assets[assets.count - 1];
    NSArray *arrayImage = [NSArray arrayWithObjects:asse, nil];
    [_selectedAssets addObjectsFromArray:arrayImage];
//    [self getOriginalImages];
//    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
 //    NSLog(@"这里有什么数据:%lu",(unsigned long)assets.count);
//    NSLog(@"有无权限:%d",[self isCanUsePhotos]);
}

实例:


浏览本地相册和照相并保存本地_第1张图片
实例.gif

你可能感兴趣的:(浏览本地相册和照相并保存本地)