2012-02-17 11 views
12

Programowo pobrałem obraz z mojego aparatu w mojej aplikacji. Ładnie zostało to pobrane, ale kiedy przejdę do innego widoku i odrzucę ten widok w tym czasie, mój obraz automatycznie obróci się o 90 stopni.Obraz z iPhone'a uchwycony z kamery obrócony automatycznie o 90 stopni

i ta zmiana pojawia się dopiero po raz pierwszy, po tym, kiedy przesunięcie nie spowoduje zmiany, oznacza to, że obraz pozostaje w stanie -90 stopni i dzieje się tak tylko wtedy, gdy obrazowałem z kamery. podczas pobierania obrazu z biblioteki zdjęć nie znaleziono problemu.

Poniższy obraz jest mój oryginalny obraz

Original image

a ten obraca obraz

rotated image

nie wiem dlaczego ta zmiana zdarzyć.

+0

zapisać ten obraz do biblioteki i sprawdzić, czy obraz jest obrócone czy nie? –

+0

@InderKumarRathore: Przechwytywam obraz z kamery urządzenia przechowywanej w bibliotece, a następnie wziąłem ten obraz w mojej aplikacji, a następnie jest on również obracany – iPhone

+0

Mam ten sam problem po wielu badaniach i znalazłem rozwiązanie sprawdź moją odpowiedź – Hiren

Odpowiedz

14

trzeba korzystać z tej funkcji, aby obrócić obraz zarejestrowany przez kamery

- (void)imagePickerController:(UIImagePickerController *)picker 
       didFinishPickingImage:(UIImage *)image 
            editingInfo:(NSDictionary *)editingInfo 
{ 
    image = [self scaleAndRotateImage:image]; 
    [self useImage:image]; 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
} 


- (void)scaleAndRotateImage:(UIImage *)image 
{ 
    int kMaxResolution = 320; // Or whatever 

    CGImageRef imgRef = image.CGImage; 

    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 
    if (width > kMaxResolution || height > kMaxResolution) { 
     CGFloat ratio = width/height; 
     if (ratio > 1) { 
      bounds.size.width = kMaxResolution; 
      bounds.size.height = bounds.size.width/ratio; 
     } 
     else { 
      bounds.size.height = kMaxResolution; 
      bounds.size.width = bounds.size.height * ratio; 
     } 
    } 

    CGFloat scaleRatio = bounds.size.width/width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
    CGFloat boundHeight; 
    UIImageOrientation orient = image.imageOrientation; 
    switch(orient) { 

     case UIImageOrientationUp: //EXIF = 1 
      transform = CGAffineTransformIdentity; 
      break; 

     case UIImageOrientationUpMirrored: //EXIF = 2 
      transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      break; 

     case UIImageOrientationDown: //EXIF = 3 
      transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationDownMirrored: //EXIF = 4 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
      transform = CGAffineTransformScale(transform, 1.0, -1.0); 
      break; 

     case UIImageOrientationLeftMirrored: //EXIF = 5 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 

     case UIImageOrientationLeft: //EXIF = 6 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 

     case UIImageOrientationRightMirrored: //EXIF = 7 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeScale(-1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 

     case UIImageOrientationRight: //EXIF = 8 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 

     default: 
      [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; 

    } 

    UIGraphicsBeginImageContext(bounds.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -height, 0); 
    } 
    else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -height); 
    } 

    CGContextConcatCTM(context, transform); 

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    [self setRotatedImage:imageCopy]; 
    //return imageCopy; 
} 
+4

prawie za tak małą rotację .__. – filou

+0

Mam ten sam problem w mojej aplikacji i próbowałem, ale to nie działa. Obraz nadal się obraca. Jaki może być problem? – Purva

+0

na czym polega problem, wyjaśnij szczegółowo: – Hiren

0

Spróbuj własność

@property (nonatomic) CGAffineTransform cameraViewTransform

i obracać się z -90 stopnia, a następnie wykonać zdjęcie. Jeśli nie działa, użyj kodu podanego przez Cocoa Matters.

0

Jeśli nadal trzeba jakieś rozwiązanie tylko odnieść this link, gdzie napisałem moją odpowiedź na ten problem tylko odnieść edytować i zmieniać 1 również zobaczyć ostatnie 2 lub 3 komentarzy dla dalszego odniesienia

4

Najbardziej Najprostszym sposobem na przezwyciężenie ten problem polega na skalowaniu obrazu wewnątrz delegata didFinishPickingImage. Możesz użyć poniższego kodu do skalowania, importując klasę "UIImage+ImageScaling.h".

theImage =[UIImage imageWithImage:image scaledToSizeWithSameAspectRatio:CGSizeMake(400, 300)];// for iphone.

-1

To dzięki swojej orientacji obrazu. więc zachowaj oryginalną orientację obrazu. dostaniesz orientację obrazu w UIImagePickerController metody delegata

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

przykładowy kod:

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

    UIImage *sourceImage = [info valueForKey:UIImagePickerControllerOriginalImage]; 

    NSData *data = UIImagePNGRepresentation(sourceImage); 
    UIImage *tmp = [UIImage imageWithData:data]; 
    UIImage *afterFixingOrientation = [UIImage imageWithCGImage:tmp.CGImage 
             scale:sourceImage.scale 
            orientation:sourceImage.imageOrientation]; 

    [self.imagePickerController dismissViewControllerAnimated:YES completion:nil]; 
} 
1

Ta metoda działa dla mnie,

- (UIImage*) rotateImageAppropriately:(UIImage*)imageToRotate 
{ 
    UIImage* properlyRotatedImage; 

    CGImageRef imageRef = [imageToRotate CGImage]; 

    if (imageToRotate.imageOrientation == 0) 
    { 
     properlyRotatedImage = imageToRotate; 
    } 
    else if (imageToRotate.imageOrientation == 3) 
    { 

     CGSize imgsize = imageToRotate.size; 
     UIGraphicsBeginImageContext(imgsize); 
     [imageToRotate drawInRect:CGRectMake(0.0, 0.0, imgsize.width, imgsize.height)]; 
     properlyRotatedImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
    else if (imageToRotate.imageOrientation == 1) 
    { 
     properlyRotatedImage = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:1]; 
    } 

    return properlyRotatedImage; 
} 
Powiązane problemy