【发布时间】:2014-06-06 13:29:58
【问题描述】:
请帮我学习objective-c!
我的程序截取了整个 UIWebView 内容,保存并显示它。 它工作正常,直到它遇到一个很长的页面。似乎页面的高度是一个问题,因为它可以显示 5 Mb 的屏幕截图,只要它们不太长(不必太高),但是 1 Mb 但极长的屏幕截图不会显示在 UIImageView一点也不。它显示 UIImageView 的背景,仅此而已。
我的代码尝试将页面大小调整为最大高度为 2000,最小为整个屏幕大小的 75%。纵横比是次要的。但是,图像仍然丢失:
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat divider = 2;
CGFloat imageDisplayLimit = 2000;
CGFloat width = _image.size.width;
CGFloat height = _image.size.height;
//determine the leading parameter (just in case of a fat-S image)
if (width > imageDisplayLimit || height > imageDisplayLimit) {
if (width > height) {
divider = width / imageDisplayLimit;
} else {
divider = height / imageDisplayLimit;
}
}
//don't let it get too fat
CGFloat minimumWidth = self.view.frame.size.width / 1.5;
if (width / divider < minimumWidth) {
width = minimumWidth;
} else {
width /= divider;
}
//don't let it get too long
CGFloat minimumHeight = self.view.frame.size.height / 1.5;
if (height / divider < minimumHeight) {
height = minimumHeight;
} else {
height /= divider;
}
_imageView = [[UIImageView alloc] initWithImage:_image];
_imageView.frame = CGRectMake(0, 0, width, height);
//now just to make it obvious - red background
_imageView.backgroundColor = [UIColor redColor];
_imageView.frame = CGRectMake(0, 0, _image.size.width / divider, _image.size.height / divider);
_imageView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin);
_imageView.contentMode = UIViewContentModeScaleAspectFill;
[_scrollView addSubview:_imageView];
_scrollView.contentSize = _imageView.bounds.size;
_scrollView.delegate = self;
//just to make scroll visible during debug
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
...
此时什么都没有显示,只有可滚动视图上的红色背景。
我正在学习objective-c,并不太了解,但是我已经尝试了2天来寻找解决方案。请救救我!
我的猜测是方面填充或适合不会做任何事情只是因为它们实际上并没有改变图像 - 只是一个显示。我真正应该使用的(恕我直言)是 UIGraphicsBeginImageContext。但我根本不知道如何让它缩小到最大 2000 像素。我可以使用
来切割它 UIGraphicsBeginImageContext(someSizeWith2000Height)
[originalImage drawInRect:CGRectMake(resized_x, resized_y, resized_width, resized_height)];
UIImage* resized_image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
或类似的东西,但我不想剪掉它 - 只是收缩(如果太短则拉伸)。
有什么想法吗?我已经用完了。
【问题讨论】:
标签: ios objective-c uiimageview uiimage