Question

Je reçois UIImages de l'appareil et de les affecter à UIImageViews à afficher. Quand je fais l'appareil photo me donne une image de 1200 x 1600 pixels que je cède alors à un UIImageView dans ma demande. L'image est affichée comme prévu dans la vue de l'image sous cette condition. Cependant, lorsque je tente de redimensionner le UIImage récupéré avant de l'assigner à l'UIImageView, l'image est redimensionne comme prévu, mais il y a un problème dans ce quelque part (dans le code RESIZING?) Mon UIImage devient TOURNÉE ... En conséquence, lorsque j'assigne la UIImage redimensionnée pour un UIImageView l'image est tournée de 90 degrés et apparaît étirée comme le rapport d'aspect (1200 x 1600 pixels) est demeuré inchangé ...

J'utilise cela pour obtenir un UIImage de l'appareil photo:

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

        myImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        myResizedImg = [self resizeImage:myImg width:400 height:533];
        [myImageView setImage:myResizedImg];

}

J'utilise ceci pour redimensionner:

-(UIImage *)resizeImage:(UIImage *)anImage width:(int)width height:(int)height
{

    CGImageRef imageRef = [anImage CGImage];

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

    if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;


    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);

    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;  
}

QUESTION: Comment puis-je Redimensionner une UIImage tirée de l'appareil photo sans faire tourner les pixels

Était-ce utile?

La solution

La raison pour laquelle votre code ne fonctionne pas parce que le imageOrientation sur le code que vous avez n'est pas pris en compte. Plus précisément, si le imageOrientation est droite / gauche, alors vous devez à la fois faire pivoter l'image et la largeur échange / hauteur. Voici un code pour faire ceci:

-(UIImage*)imageByScalingToSize:(CGSize)targetSize
{
    UIImage* sourceImage = self; 
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    }   

    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -targetWidth, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return newImage; 
}

Ceci redimensionner votre image et faites-le pivoter à l'orientation correcte. Si vous avez besoin pour la définition radians, il est:

static inline double radians (double degrees) {return degrees * M_PI/180;}

La réponse Daniel a donné est également correcte, mais il souffre du problème qu'il est thread-safe, puisque vous utilisez UIGraphicsBeginImageContext (). Étant donné que le code ci-dessus utilise uniquement des fonctions CG, vous êtes tous ensemble. J'ai aussi une fonction similaire à redimensionner et faire remplir aspect approprié sur les images - faites-moi savoir si c'est ce que vous cherchez

.

Note: Je suis la fonction originale de

Autres conseils

J'ai eu ce problème aussi avec une partie du code là-bas, je l'ai trouvé ce code qui fonctionne, vérifier, laissez-moi savoir ce que vous trouvez

+ (UIImage*)imageWithImage:(UIImage*)image 
               scaledToSize:(CGSize)newSize;
{
   UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   return newImage;
}

Swift 3

J'ajoute cela à didFinishPickingMediaWithInfo méthode et utiliser ensuite image sans se soucier de sa rotation.

var image = info[UIImagePickerControllerOriginalImage] as! UIImage
if (image.imageOrientation != .up) {
  UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
  image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  image = UIGraphicsGetImageFromCurrentImageContext()!
  UIGraphicsEndImageContext()
}

Si vous voulez faire une image carrée d'une image rectangle (peu importe horizontale ou verticale) par culture par largeur ou la hauteur des deux côtés, utiliser mon code. La différence est que je ne suis pas étirer, mais je récolte. Je l'ai fait à partir du code dessus par modification:

//Cropping _image to fit it to the square by height or width

CGFloat a = _image.size.height;
CGFloat b = _image.size.width;
CGRect cropRect;

if (!(a==b)) {
    if (a<b) {
        cropRect = CGRectMake((b-a)/2.0, 0, a, a);
        b = a;
    }
    else if (b<a) {
        cropRect = CGRectMake(0, (a-b)/2.0, b, b);
        a = b;
    }

    CGImageRef imageRef = CGImageCreateWithImageInRect([_image CGImage], cropRect);

    UIImage* sourceImage = _image; 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
    CGContextRef bitmap;
    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {
        bitmap = CGBitmapContextCreate(NULL, a, a, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
    }

    if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -a);

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -a, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        CGContextTranslateCTM (bitmap, a, a);
        CGContextRotateCTM (bitmap, radians(-180.));
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, a, a), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    _image = [UIImage imageWithCGImage:ref];
    CGImageRelease(imageRef);
}
scroll top