0

I'm trying to take a photo using iPhone camera and save it to both document folder and Photo Album and load this image in another scene. Here is my code:

For image saving (in scene A):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.ImageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];

    NSString *mediaType = info[UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

        NSString *fileName = [NSString stringWithFormat:@"%@.png", self.patientMRN];
        NSURL *urlSave = [[self getDocumentsPathURL] URLByAppendingPathComponent:fileName];

        [UIImagePNGRepresentation(chosenImage) writeToFile:urlSave.path atomically:YES];

        UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);

    }

}

And for image loading (in scene B):

-(void)loadDefaultImage
{
    NSString *fileName = [NSString stringWithFormat:@"%@.png", self.patientMRN];
    NSURL *urlImage = [[self getDocumentsPathURL] URLByAppendingPathComponent:fileName];

    if ([[NSFileManager defaultManager] fileExistsAtPath:urlImage.path]) {
    NSData *dataFile = [[NSFileManager defaultManager] contentsAtPath:urlImage.path];
    UIImage *image = [[UIImage alloc] initWithData:dataFile];

    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.autoresizingMask =
    ( UIViewAutoresizingFlexibleBottomMargin
     | UIViewAutoresizingFlexibleHeight
     | UIViewAutoresizingFlexibleLeftMargin
     | UIViewAutoresizingFlexibleRightMargin
     | UIViewAutoresizingFlexibleTopMargin
     | UIViewAutoresizingFlexibleWidth );
    self.imageView.image = image;
    }
    else {
       NSLog(@"Image not found");
    }
}

Now i have two problems.

  1. In scene A, i have an UIImageView for which i set height and width constraints. Below the UIImageView i have two buttons, one for take photo and one for going back. After taking the photo and choose use photo, it'll go back to scene A and display the captured image in the UIImageView. However, the image displayed got so big it partially overlaps two buttons beneath it, as if the constraints i set doesn't work at all. Why does this happen?

  2. In scene B, i have a smaller UIImageView used to display the photo i took earlier. However, only part of the original image is shown in scene B's imageView. I tried to set the contentMode as UIViewContentModeScaleAspectFill and set the autoresizingMask, but still doesn't work. Where does it go wrong?

Edit:

The Problem is solved! After i dragged the UIImageview to the storyboard, i just resize it to fit the screen, but forgot to add height and width constraints to it. Now i add the constraints, the image is displayed as expected. Thank you guys.

Min Tang
  • 11
  • 3

1 Answers1

0

Have you tried to set not only contentMode property, but also clipsToBounds?

self.imageView.contentMode   = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;
keyv
  • 2,430
  • 1
  • 18
  • 15