UIImagePickerController에서 이미지 크기 조정이 작동하지 않음 (Resizing an image from UIImagePickerController not working)


문제 설명

UIImagePickerController에서 이미지 크기 조정이 작동하지 않음 (Resizing an image from UIImagePickerController not working)

I am following this post on how to resize an image. I placed the + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize; in selectExerciseImageViewController.h and copied the relevant code to selectExerciseImageViewController.m.

Then I attempt to resize the image, save it, and retrieve the saved image file into a UIImageView. but for some reason the image is never showing the the UIImageView and the last NSLog returns null

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

    [picker dismissModalViewControllerAnimated:YES];

  UIImage* newImage = [selectExerciseImageViewController imageWithImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"]
      scaledToSizeWithSameAspectRatio:CGSizeMake(40.0,40.0)];

    NSData* imageData = UIImagePNGRepresentation(newImage);
    // Give a name to the file
    NSString* imageName = @"MyImage.png";

    // Now, we have to find the documents directory so we can save it
    // Note that you might want to save it elsewhere, like the cache directory,
    // or something similar.
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];

    // Now we get the full path to the file
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

    // and then we write it out
    [imageData writeToFile:fullPathToFile atomically:NO];
    //imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _testimg.image = [UIImage imageNamed:@"MyImage.png"];
    NSLog(@"asd %@",[UIImage imageNamed:@"MyImage.png"]);

}

참조 솔루션

방법 1:

[UIImage imageNamed:@"MyImage.png"];

This loads an image from the main application bundle.  You are writing the file to the documents directory.  You need to instantiate the UIImage object with a different method.  Try:

[UIImage imageWithContentsOfFile:fullPathToFile];

(by Jonathan ThurftJim)

참조 문서

  1. Resizing an image from UIImagePickerController not working (CC BY‑SA 3.0/4.0)

#uiimagepickercontroller #iOS #cocoa-touch






관련 질문

UIImagePickerController 닫기 (Dismiss UIImagePickerController)

UIImagePickerController에서 이미지 크기 조정이 작동하지 않음 (Resizing an image from UIImagePickerController not working)

사진 촬영 후 사진 사용 또는 재촬영을 선택할 수 없습니다. (After Taking Picture cannot select Use Photo or Retake)

런타임 오류를 제공하는 UIImagePicker (UIImagePicker giving runtime error)

Swift:UIImagePickerController에서 버튼의 텍스트를 변경하는 방법은 무엇입니까? (Swift: how to change the text of buttons in UIImagePickerController?)

iOS 9에서 UIImagePickerController가 이미지를 선택하지 않음 (UIImagePickerController not picking image in iOS 9)

UIImagePNGRepresentation() 내 UIImage 회전 (UIImagePNGRepresentation() rotate my UIImage)

UIImagePickerController에서 편집 사각형을 구성할 수 있습니까? (Can we config edit rectangle in UIImagePickerController)

imagePickerController를 닫는 방법? (how to close imagePickerController?)

카메라 사용 설명/사진 라이브러리 사용 설명을 추가해도 경고가 표시되지 않음 (Not showing alert even when I add Camera Usage Description / Photo Library Usage Description)

UIImagePIcker를 통해 "JPG", "PNG" 및 "JPEG" 이미지만 선택할 수 있는 방법이 있습니까? (Is there any way by which I can select only "JPG", "PNG" and "JPEG" images through UIImagePIcker)

사진 라이브러리 Swift에 비디오 저장 (Save video in Photo Library Swift)







코멘트