【问题标题】:How can I avoid saving to camera roll an image picked from camera roll ? [iOS]如何避免将从相机胶卷中挑选的图像保存到相机胶卷? [iOS]
【发布时间】:2013-01-18 18:48:05
【问题描述】:

我可以轻松地将用相机拍摄的图像保存到相机胶卷。 但如果这是从相机胶卷中选择的,我不想保存图像,因为它会创建一个副本。

如何执行以下操作并有选择地保存到相机胶卷?

编辑 1: 正如 rmaddy 所说,我可以: 仅当 picker.sourceType 等于 UIImagePickerControllerSourceTypeCamera 时才保存到相机胶卷。

但如果我这样做,我将无法获取所选图像的信息,主要是文件名。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self.myImageView setImage: image];
    [self.myImageView setContentMode:UIViewContentModeScaleAspectFit];
    ALAssetsLibrary *al=[[ALAssetsLibrary alloc] init];

    // ------> Saving to Camera Roll <------------

    [al writeImageToSavedPhotosAlbum:[image CGImage] metadata:nil completionBlock:^(NSURL *assetURL,NSError *error) { 

            ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myAsset) {
                ALAssetRepresentation *rep = [myAsset defaultRepresentation];
                NSString *fileName = [rep filename];
                ImageInfo *imgInfo = [[ImageInfo alloc] initWithEventId:0 name:fileName image:UIImageJPEGRepresentation(image, 1.0)];
                imgInfo.isDirty = YES;
                [self.imagesArray addObject:imgInfo];
                [self dismissViewControllerAnimated:YES completion:nil];
                NSLog(@"Image from Camera %@ added to imageArray",fileName);
            };

            ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
            [assetsLibrary assetForURL:assetURL
                           resultBlock:resultblock
                          failureBlock:nil];

     }];
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}

编辑2:

用这个 MOD 解决了: (知道有一点代码重复,以后会努力改进的)

if (newMedia){
        ALAssetsLibrary *al=[[ALAssetsLibrary alloc] init];
        [al writeImageToSavedPhotosAlbum:[image CGImage] metadata:nil completionBlock:^(NSURL *assetURL,NSError *error)
        {                
            ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myAsset)
            {
                ALAssetRepresentation *rep = [myAsset defaultRepresentation];
                //NSString *fileName = [rep filename];
                NSString *imgUrl = [[rep url] absoluteString];
                ImageInfo *imgInfo = [[ImageInfo alloc] initWithEventId:0 imgUrl:imgUrl image:UIImageJPEGRepresentation(image, 1.0)];
                imgInfo.isDirty = YES;
                [self.imagesArray addObject:imgInfo];
                [self dismissViewControllerAnimated:YES completion:nil];
                NSLog(@"Image from Camera %@ added to imageArray",imgUrl);
            };

            ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
            [assetsLibrary assetForURL:assetURL
                           resultBlock:resultblock
                          failureBlock:nil];
        }];
} else {
        NSString* imgUrl = UIImagePickerControllerReferenceURL;
        ImageInfo *imgInfo = [[ImageInfo alloc] initWithEventId:0 imgUrl:imgUrl image:UIImageJPEGRepresentation(image, 1.0)];
        imgInfo.isDirty = YES;
        [self.imagesArray addObject:imgInfo];
        NSLog(@"Image from Camera Roll %@ added to imageArray",imgUrl);
        [self dismissViewControllerAnimated:YES completion:nil];
}

【问题讨论】:

    标签: ios objective-c uiimagepickercontroller photo-gallery alasset


    【解决方案1】:

    仅当picker.sourceType 等于UIImagePickerControllerSourceTypeCamera 时才保存到相机胶卷。

    【讨论】:

    • 对,但是如果我这样做,我将无法获取所选图像的信息,主要是文件名。
    • @AlvinPastore 对info 字典使用UIImagePickerControllerReferenceURL 键来获取所选图像的原始URL。从照片库中获取图像时,使用它而不是 assetURL。
    • 谢谢。我决定使用 URL 而不是文件名并修改代码以直接处理信息(如您所建议的)而不是使用 ALAssetLibrary 代码...
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    相关资源
    最近更新 更多