MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:image]];
我的UIImage
是rectangle
,当我使用MPNowPlayingInfoCenter
将其放到MPMediaItemArtwork
时,它会被裁剪成正方形。如何调整UIImage
的大小以适应宽度square UIImage
并将其余部分留空?
答案 0 :(得分:2)
将UIImage
放入UIImageView
,contentMode
设为UIImageView
为UIViewContentModeScaleAspectFit
并将其呈现回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;
}