UIImagePNGRepresentation … writeToFile is always landscape

后端 未结 1 559
误落风尘
误落风尘 2021-01-13 19:51

Each time I use the camera to take a photograph, then save it the image is always in landscape. This means the UIImageView in my Xib is the wrong way around. Its Portrait -

相关标签:
1条回答
  • 2021-01-13 20:37

    I found this post to be very helpful, it describes adding a category to UIImage to do the rotation of the image before you save it:

    http://www.platinumball.net/blog/2009/03/30/iphone-uiimage-rotation-and-mirroring/

    And then once the category is implemented, you would do something like this:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    {
        UIImage *originalImage, *editedImage, *rotatedImage;
    
        editedImage = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
        originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
    
        if (editedImage) 
        {
            rotatedImage = [editedImage rotate:UIImageOrientationRight];
        } 
        else 
        {
            rotatedImage = [originalImage rotate:UIImageOrientationRight];
        }
    
        NSString *f = @"/set/this/to/your/file/name"; 
        [UIImagePNGRepresentation(rotatedImage) writeToFile:f atomically:YES];
    
        [[picker parentViewController] dismissModalViewControllerAnimated:YES];
        [picker release];
    }
    
    0 讨论(0)
提交回复
热议问题