第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

調(diào)整從相機(jī)拉出的UIimage的大小也會旋轉(zhuǎn)UIimage嗎?

調(diào)整從相機(jī)拉出的UIimage的大小也會旋轉(zhuǎn)UIimage嗎?

iOS
小怪獸愛吃肉 2019-12-18 16:17:41
我從相機(jī)獲取UIimage,并將它們分配給UIImageViews進(jìn)行顯示。當(dāng)我這樣做時(shí),相機(jī)會給我一個(gè)1200 x 1600像素的圖像,然后將其分配給我的應(yīng)用程序中的UIImageView。在這種情況下,圖像將按預(yù)期顯示在圖像視圖中。但是,當(dāng)我嘗試在將檢索到的UIImage分配給UIImageView之前重新調(diào)整其大小時(shí),該圖像正在按預(yù)期調(diào)整大小,但是在某個(gè)地方(在RESIZING代碼中)存在一個(gè)問題,我的UIImage變得旋轉(zhuǎn)了...結(jié)果,當(dāng)我將調(diào)整大小的UIImage分配給UIImageView時(shí),圖像將旋轉(zhuǎn)90度并顯示為拉伸,因?yàn)榭v橫比(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];}我正在使用它來調(diào)整它的大小:-(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;  }問題:如何在不旋轉(zhuǎn)像素的情況下調(diào)整從相機(jī)拉出的UIImage的大???
查看完整描述

3 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

您的代碼不起作用的原因是因?yàn)闆]有考慮您所擁有的代碼上的imageOrientation。具體來說,如果imageOrientation為右/左,則需要同時(shí)旋轉(zhuǎn)圖像和交換寬度/高度。這是執(zhí)行此操作的一些代碼:


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

}

這將調(diào)整圖像的大小并將其旋轉(zhuǎn)到正確的方向。如果需要弧度的定義,則為:


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

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


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

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

我那里的一些代碼也遇到了這個(gè)問題,我發(fā)現(xiàn)此代碼有效,請檢查一下,讓我知道您找到了什么


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

               scaledToSize:(CGSize)newSize;

{

   UIGraphicsBeginImageContext( newSize );

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

   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();


   return newImage;

}


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

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

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


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

}


查看完整回答
反對 回復(fù) 2019-12-18
  • 3 回答
  • 0 關(guān)注
  • 593 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號