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

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

UIImage:調整大小,然后裁剪

UIImage:調整大小,然后裁剪

侃侃爾雅 2019-07-09 16:40:23
UIImage:調整大小,然后裁剪我已經對這張臉猛擊了好幾天,盡管我一直覺得自己正處在啟示錄的邊緣,但我根本無法實現我的目標。我認為,在我設計的概念階段之前,從iPhone的相機或庫中獲取圖像,將其縮小到指定的高度,使用與側面填充選項UIImageView(完全在代碼中),然后割掉任何不符合通過的CGRect的內容。從相機或圖書館獲取原始圖像是微不足道的。事實證明,其他兩個步驟是多么困難,我對此感到震驚。附圖顯示了我想要達到的目標。有人能幫我握住我的手嗎?到目前為止,我發現的每一個代碼示例似乎都會破壞圖像,使之顛倒,看起來像垃圾,超出界限,否則就不能正常工作。
查看完整描述

3 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

我需要同樣的東西-在我的例子中,選擇適合的尺寸,一旦縮放,然后裁剪每一端,以適應其余的寬度。(我的工作是景觀,所以可能沒有注意到肖像模式的任何缺陷。)這是我的代碼-這是UIImage上的一部份。我的代碼中的目標大小總是設置為設備的全屏大小。

@implementation UIImage (Extras)#pragma mark -#pragma mark Scale and crop image- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;    
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor) 
        {
            scaleFactor = widthFactor; // scale to fit height
        }
        else
        {
            scaleFactor = heightFactor; // scale to fit width
        }

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        }
        else
        {
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }
    }   

    UIGraphicsBeginImageContext(targetSize); // this will crop

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil)
    {
        NSLog(@"could not scale image");
    }

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    return newImage;}



查看完整回答
反對 回復 2019-07-09
?
滄海一幻覺

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

舊的帖子包含用于調整UIImage大小的方法的代碼。有關部分如下:

+ (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-07-09
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

+ (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)targetSize {
    //If scaleFactor is not touched, no scaling will occur      
    CGFloat scaleFactor = 1.0;

    //Deciding which factor to use to scale the image (factor = targetSize / imageSize)
    if (image.size.width > targetSize.width || image.size.height > targetSize.height)
        if (!((scaleFactor = (targetSize.width / image.size.width)) > (targetSize.height / image.size.height))) //scale to fit width, or
            scaleFactor = targetSize.height / image.size.height; // scale to fit heigth.

    UIGraphicsBeginImageContext(targetSize); 

    //Creating the rect where the scaled image is drawn in
    CGRect rect = CGRectMake((targetSize.width - image.size.width * scaleFactor) / 2,
                             (targetSize.height -  image.size.height * scaleFactor) / 2,
                             image.size.width * scaleFactor, image.size.height * scaleFactor);

    //Draw the image into the rect
    [image drawInRect:rect];

    //Saving the image, ending image context
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return scaledImage;}

我提議這個。她不漂亮嗎?)


查看完整回答
反對 回復 2019-07-09
  • 3 回答
  • 0 關注
  • 705 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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