Question

I can easily save to camera roll images taken with camera. But I don't want to save the image if this is chosen from the camera roll, because it will create a duplicate.

How can I do the following and selectively saving to camera roll?

EDIT 1: As rmaddy said I can: Only save to the camera roll if picker.sourceType is equal to UIImagePickerControllerSourceTypeCamera.

but if I do that, I won't be able to get the info of the selected image, the filename mainly.

-(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
}

EDIT 2:

SOLVED WITH THIS MOD: (I know that there is a little bit of code duplication, I will try to improve it later)

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];
}
Was it helpful?

Solution

Only save to the camera roll if picker.sourceType is equal to UIImagePickerControllerSourceTypeCamera.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top