亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

調整從相機拉出的UIimage的大小也會旋轉UIimage嗎?

調整從相機拉出的UIimage的大小也會旋轉UIimage嗎?

iOS
小怪獸愛吃肉 2019-12-18 16:17:41
我從相機獲取UIimage,并將它們分配給UIImageViews進行顯示。當我這樣做時,相機會給我一個1200 x 1600像素的圖像,然后將其分配給我的應用程序中的UIImageView。在這種情況下,圖像將按預期顯示在圖像視圖中。但是,當我嘗試在將檢索到的UIImage分配給UIImageView之前重新調整其大小時,該圖像正在按預期調整大小,但是在某個地方(在RESIZING代碼中)存在一個問題,我的UIImage變得旋轉了...結果,當我將調整大小的UIImage分配給UIImageView時,圖像將旋轉90度并顯示為拉伸,因為縱橫比(1200 x 1600像素)未更改...我正在使用它從Camera獲取UIImage:- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{        myImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];        myResizedImg = [self resizeImage:myImg width:400 height:533];        [myImageView setImage:myResizedImg];}我正在使用它來調整它的大小:-(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;  }問題:如何在不旋轉像素的情況下調整從相機拉出的UIImage的大???
查看完整描述

3 回答

?
慕哥6287543

TA貢獻1831條經驗 獲得超10個贊

您的代碼不起作用的原因是因為沒有考慮您所擁有的代碼上的imageOrientation。具體來說,如果imageOrientation為右/左,則需要同時旋轉圖像和交換寬度/高度。這是執行此操作的一些代碼:


-(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; 

}

這將調整圖像的大小并將其旋轉到正確的方向。如果需要弧度的定義,則為:


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

Daniel給出的答案也是正確的,但是由于您使用的是UIGraphicsBeginImageContext(),因此它存在線程不安全的問題。由于上述代碼僅使用CG功能,因此已全部準備就緒。我還有一個類似的功能,可以調整大小并在圖像上進行適當的縱橫比填充-讓我知道這是否是您要的內容。


查看完整回答
反對 回復 2019-12-18
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

我那里的一些代碼也遇到了這個問題,我發現此代碼有效,請檢查一下,讓我知道您找到了什么


+ (UIImage*)imageWithImage:(UIImage*)image 

               scaledToSize:(CGSize)newSize;

{

   UIGraphicsBeginImageContext( newSize );

   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();


   return newImage;

}


查看完整回答
反對 回復 2019-12-18
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

如果要通過在矩形圖像(水平或垂直)中通過在兩側的寬度或高度進行裁剪來制作正方形圖像,請使用我的代碼。所不同的是,我不會伸展,但會收割。我通過修改從最上面的代碼中獲得了它:


//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);

}


查看完整回答
反對 回復 2019-12-18
  • 3 回答
  • 0 關注
  • 567 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號