How to load photos from photo gallery and store it into application project?

前端 未结 3 1135
独厮守ぢ
独厮守ぢ 2021-02-01 09:13

I\'m working on an application that is almost the same as the sample codes i found here. But the method i want to do is different from the sample codes.
Link: PhotoLocations

3条回答
  •  臣服心动
    2021-02-01 09:44

    This will take you to the image gallery and you can select the image.

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    

    this will help you select the image

    - (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
    {
        // Dismiss the image selection, hide the picker and
    
        //show the image view with the picked image
    
        [picker dismissViewControllerAnimated:YES completion:nil];
        //UIImage *newImage = image;
    }
    

    And then you can store this image to the documents directory...

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *image = imageView.image; // imageView is my image from camera
    NSData *imageData = UIImagePNGRepresentation(image);
    [imageData writeToFile:savedImagePath atomically:NO];
    

    For clicking the image yourself use this

    - (IBAction) takePhoto:(id) sender
    {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    
        imagePickerController.delegate = self;
        imagePickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;
    
        [self presentModalViewController:imagePickerController animated:YES];
    }
    

提交回复
热议问题