欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS拍照后使用该图片(获取照片的ALasset传到相册)

程序员文章站 2024-01-15 19:18:46
...

问题:
拍照后,点击选择照片在-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 的代理方法中获取了相册的Alasset后,发现为空

解决办法:

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
     ALAssetsLibrary *library  = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
            if (error) {
                // TODO: error handling
            } else {
                [library  assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                    [self.selectPhotos addObject:asset];
                    [_imageCollection reloadData];
                } failureBlock:^(NSError *error) {

                }];
            }
        }];
    }
}