2016-11-02 47 views

回答

1

UIImageUIImageView,設置contentModeUIImageViewUIViewContentModeScaleAspectFit,並使其回到UIImage

+ (UIImage *)imageToSquare:(UIImage *)image byWidth:(float)width{ 
     UIImageView *view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)]; 
     [view setImage:image]; 
     view.contentMode = UIViewContentModeScaleAspectFit; 

     UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); 
     [view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

     UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     return img; 
    }