【发布时间】:2016-02-26 18:00:25
【问题描述】:
我在使用 UIImageView 时遇到了一个非常奇怪的问题,我试图将图像(通过截取当前视图的屏幕截图创建)设置为内容模式为 UIViewContentModeScaleAspectFit 的 ImageView。
当我通过 xib 文件中的界面生成器设置图像或设置由 [UIImage imageNamed:] 创建的图像时,它工作正常。他们都可以很好地使用 UIViewContentModeScaleAspectFit。
但是当我拍摄视图的快照并将图像设置为图像视图时,图像不适合 UIImageView。我已经尝试了我在这里找到的所有解决方案,例如 .ClipsToBound = YES 但它们根本不起作用。我现在真的很困惑。
这是我截屏并创建 UIImage 时的代码:
- (UIImage *)screenshotWithRect:(CGRect)captureRect
{
CGFloat scale = [[UIScreen mainScreen] scale];
UIImage *screenshot;
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, scale);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureRect);
{
if(UIGraphicsGetCurrentContext() == nil)
{
NSLog(@"UIGraphicsGetCurrentContext is nil. You may have a UIView (%@) with no really frame (%@)", [self class], NSStringFromCGRect(self.frame));
}
else
{
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
screenshot = UIGraphicsGetImageFromCurrentImageContext();
}
}
UIGraphicsEndImageContext();
return screenshot;
}
当我将图像设置为图像视图时
UIImage* snap = [[UIImage alloc] init];
// start snap shot
UIView* superView = [self.view superview];
CGRect cutRect = [superView convertRect:self.cutView.frame fromView:_viewToCut];
snap = [superView screenshotWithRect:cutRect];
[self.view addSubview:self.editCutFrameView];
// end snap shot -> show edit view
[self.editCutFrameView setImage:snap];
非常感谢您的帮助。
更新:正如@Saheb Roy 提到的大小,我检查了图像大小,它大约是 400x500 像素,thumbnail.png 的大小是 512x512 像素,所以我认为这与图像的大小无关。
【问题讨论】:
标签: ios objective-c uiimageview uiimage