Save image in UIImageView to iPad Photos Library

前端 未结 4 1186
死守一世寂寞
死守一世寂寞 2020-12-31 22:40

I am creating an iPad app that has several pictures (UIImageViews) in a horizontal scrollview. I want to allow the user to be able to save the images to their P

4条回答
  •  鱼传尺愫
    2020-12-31 23:18

    - (IBAction)TakePicture:(id)sender {
    
    
        // Create image picker controller
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
        // Set source to the camera
        imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
    
    
        // Delegate is self
        imagePicker.delegate = self;
    
    
        OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    
       // imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
    
        // Insert the overlay:
        imagePicker.cameraOverlayView = overlay;
    
       // Allow editing of image ?
        imagePicker.allowsImageEditing = YES;
        [imagePicker setCameraDevice:
         UIImagePickerControllerCameraDeviceFront];
        [imagePicker setAllowsEditing:YES];
        imagePicker.showsCameraControls=YES;
        imagePicker.navigationBarHidden=YES;
        imagePicker.toolbarHidden=YES;
        imagePicker.wantsFullScreenLayout=YES;
    
    
        self.library = [[ALAssetsLibrary alloc] init];
    
    
        // Show image picker
        [self presentModalViewController:imagePicker animated:YES];
    }
    
    
    
    
    
    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        // Access the uncropped image from info dictionary
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    
    
    
        // Save image to album
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    
        // save image to custom album
        [self.library saveImage:image toAlbum:@"custom name" withCompletionBlock:^(NSError *error) {
            if (error!=nil) {
                NSLog(@"Big error: %@", [error description]);
            }
        }];
    
        [picker dismissModalViewControllerAnimated:NO];
    
    
    }
    
    
    
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
        UIAlertView *alert;
    
        // Unable to save the image  
        if (error)
            alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                               message:@"Unable to save image to Photo Album." 
                                              delegate:self cancelButtonTitle:@"Ok" 
                                     otherButtonTitles:nil];
        else // All is well
            alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                               message:@"Image saved to Photo Album." 
                                              delegate:self cancelButtonTitle:@"Ok" 
                                     otherButtonTitles:nil];
    
    
        [alert show];
    }
    
    
    
    - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        // After saving iamge, dismiss camera
        [self dismissModalViewControllerAnimated:YES];
    }
    

提交回复
热议问题