Get width of a resized image after UIViewContentModeScaleAspectFit

前端 未结 6 1645
忘了有多久
忘了有多久 2021-01-30 13:56

I have my UIImageView and I put an image into it that I resize like this:

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 14:53

    According to Apple's Documentation for UIViewContentModeScaleAspectFit

    It Scales the content (In your case actualImage) to fit the size of the view (In your case attachmentImageNew) by maintaining the aspect ratio.

    That means your image size ( after the scaling ) should be same as your UIImageView.

    UPDATE :

    One new Suggestion to you if you don't want to use UIViewContentModeScaleAspectFit and if you can fix the size of scaledImage then you can use below code to scale the image for fix newSize and then you can use the width to your code.

    CGSize newSize = CGSizeMake(100.0,50.0);
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

提交回复
热议问题