如何避免保存到相机胶卷从相机胶卷中拾取的图像? [iOS的]

时间:2013-01-18 18:48:06

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

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

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

编辑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];
}

1 个答案:

答案 0 :(得分:3)

如果picker.sourceType等于UIImagePickerControllerSourceTypeCamera,则只保存到相机胶卷。