2015-09-24 1 views
0

Моего размера изображения 2000x1900, и мой взгляд изображения размера 500x500.When я установить изображение в изображении просмотреть его Поворот image.I также используетсяКак поместить большое изображение в изображение без его поворота?

contentMode = UIViewContentModeScaleAspectFill; 
clipsToBounds = YES; 

Проверить изображение orientation.It является UIImageOrientationUp.But еще я получить поворот изображения. Так кто-нибудь поможет мне в чем причина и как я могу решить эту проблему?

+0

Установка изображения в ImageView не должен поворачивать образ сам по себе. Как вы добавляете изображение? Каков его источник? Используете ли вы фильтры основного изображения или что-то еще, что изменяет данные изображения? –

ответ

1

это может быть помощь для вас ..

Если я понимаю, что вы хотите сделать, это игнорировать ориентацию UIImage? Если да, то вы можете сделать это:

UIImage *originalImage = [... whatever ...]; 

UIImage *imageToDisplay = 
    [UIImage imageWithCGImage:[originalImage CGImage] 
       scale:1.0 
       orientation: UIImageOrientationUp]; 
1

Вы можете использовать ниже метод изменить размер изображения

-(UIImage *)compressImage:(UIImage *)image 
{ 
    float actualHeight = image.size.height; 
    float actualWidth = image.size.width; 
    float maxHeight = 500; 
    float maxWidth = 500; 
    float imgRatio = actualWidth/actualHeight; 
    float maxRatio = maxWidth/maxHeight; 
    float compressionQuality = 1;//50 percent compression 

    if (actualHeight > maxHeight || actualWidth > maxWidth){ 
     if(imgRatio < maxRatio){ 
      //adjust width according to maxHeight 
      imgRatio = maxHeight/actualHeight; 
      actualWidth = imgRatio * actualWidth; 
      actualHeight = maxHeight; 
     } 
     else if(imgRatio > maxRatio){ 
      //adjust height according to maxWidth 
      imgRatio = maxWidth/actualWidth; 
      actualHeight = imgRatio * actualHeight; 
      actualWidth = maxWidth; 
     } 
     else{ 
      actualHeight = maxHeight; 
      actualWidth = maxWidth; 
     } 
    } 
    actualHeight = 2000; 
    actualWidth = 1900; 

    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); 
    UIGraphicsBeginImageContext(rect.size); 
    [image drawInRect:rect]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality); 
    UIGraphicsEndImageContext(); 

    return [UIImage imageWithData:imageData]; 
} 

В выше метод набора высоты-ширины согласно вашему требованию. Надеюсь, это полезно для вас.

0

Попробуйте ввести код для исправления проблемы с установкой.

UIImage + fixOrientation.h

@interface UIImage (fixOrientation) 

- (UIImage *)fixOrientation; 

@end 

UIImage + fixOrientation.m

@implementation UIImage (fixOrientation) 

- (UIImage *)fixOrientation { 

    // No-op if the orientation is already correct 
    if (self.imageOrientation == UIImageOrientationUp) return self; 

    // We need to calculate the proper transformation to make the image upright. 
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 
    CGAffineTransform transform = CGAffineTransformIdentity; 

    switch (self.imageOrientation) { 
     case UIImageOrientationDown: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 

     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
      transform = CGAffineTransformRotate(transform, M_PI_2); 
      break; 

     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, 0, self.size.height); 
      transform = CGAffineTransformRotate(transform, -M_PI_2); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationUpMirrored: 
      break; 
    } 

    switch (self.imageOrientation) { 
     case UIImageOrientationUpMirrored: 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.width, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 

     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRightMirrored: 
      transform = CGAffineTransformTranslate(transform, self.size.height, 0); 
      transform = CGAffineTransformScale(transform, -1, 1); 
      break; 
     case UIImageOrientationUp: 
     case UIImageOrientationDown: 
     case UIImageOrientationLeft: 
     case UIImageOrientationRight: 
      break; 
    } 

    // Now we draw the underlying CGImage into a new context, applying the transform 
    // calculated above. 
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, 
              CGImageGetBitsPerComponent(self.CGImage), 0, 
              CGImageGetColorSpace(self.CGImage), 
              CGImageGetBitmapInfo(self.CGImage)); 
    CGContextConcatCTM(ctx, transform); 
    switch (self.imageOrientation) { 
     case UIImageOrientationLeft: 
     case UIImageOrientationLeftMirrored: 
     case UIImageOrientationRight: 
     case UIImageOrientationRightMirrored: 
      // Grr... 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); 
      break; 

     default: 
      CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); 
      break; 
    } 

    // And now we just create a new UIImage from the drawing context 
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
    UIImage *img = [UIImage imageWithCGImage:cgimg]; 
    CGContextRelease(ctx); 
    CGImageRelease(cgimg); 
    return img; 
} 

@end 

Использование в качестве

#import "UIImage+fixOrientation.h" 

UIImage *image = [yourImage fixOrientation]; 
yourImageView.image = image;