我加载了一个jpg到UIImageView中。图像过大到iPhone屏幕。我怎样才能调整它到一个特定的CGRect框架?如何在UIImageView中调整JPG大小?
UIImageView *uivSplash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPhone-Splash.jpg"]];
[self.view addSubview:uivSplash];
我加载了一个jpg到UIImageView中。图像过大到iPhone屏幕。我怎样才能调整它到一个特定的CGRect框架?如何在UIImageView中调整JPG大小?
UIImageView *uivSplash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iPhone-Splash.jpg"]];
[self.view addSubview:uivSplash];
一个UIImageView
只是一个UIView
,所以你可以改变它的frame
属性。
uivSplash.frame = CGRectMake(0, 0, width, height);
你会想等的方法如下:
CGFloat newWidth = whateverYourDesiredWidth; // someView.size.width for example
CGFloat newHeight = whateverYourDesiredHeight; // someView.size.height for example
CGSize newSize = CGSizeMake(newWidth, newHeight);
UIGraphicsBeginImageContext(newSize);
[yourLargeImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
因此,这是越来越所需的宽度和高度(也许是屏幕尺寸,也许硬编码的大小,也许大小基于一个UIView),并在这个尺寸的上下文中重新绘制图像。
〜好运
编辑:它发生到我,我可能误解了你的愿望,所以我也将指出(如其他人所说的),其具有的UIImageView,让你把它适合于自己的形象性尺寸,填充比例,保留长宽比等。
检查右侧的相关框:此问题已回答100次。 – vikingosegundo 2012-01-11 02:41:23