UIWebView内容大小

发布于 2024-10-27 14:31:37 字数 1297 浏览 6 评论 0原文

我正在开发一个可以从网络检索图像的应用程序。我没有实际下载图像,而是尝试在 UIWebView 中显示它们。我确实需要图像在屏幕上具有特定的尺寸,所以我制作了一个我需要的尺寸的网络视图,并告诉它scalesPageToFit,但是当我运行时,图像在视图中的尺寸不正确。我使用 1024x768 图像作为测试,并尝试了多次迭代视图尺寸以使其在屏幕上正确显示,但最终要么得到不合逻辑的尺寸,要么出现剪辑。

这是原始图像; http://www.coolopticalillusions.com/backgrounds/freaky-desktop-background -7.jpg

我试图将图像设置为 300 宽,这会保留纵横比,这意味着视图应该为 245 高。

第一个屏幕截图是使用 300x245 UIWebView,您可以看到宽度是正确的,但高度不正确,红色框应该与图像底部完全对齐 http://img839.imageshack.us/f/sizecropped.png/

这一秒屏幕截图的分辨率为 300x300,对于视图而言,图像缩放得太宽,也太高。 http://img155.imageshack.us/f/sizetoobig.png/

我有我已经挣扎了一段时间试图让它做我想做的事,但我只是没有取得任何成功。

编辑:我拥有的唯一相关代码是:

NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"http://www.coolopticalillusions.com/backgrounds/freaky-desktop-background-7.jpg"];
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[messageView.imageWebView loadRequest:urlRequest];
messageView.imageWebView.scalesPageToFit = YES;

I'm working on an app that can retrieve images from the web. Rather than actually download the images, I'm trying to display them in a UIWebView. I really need the images to be a specific size on my screen, so I have made a webview of the size I need, and told it to scalesPageToFit, but when I run, the image is not the correct size within the view. I am using a 1024x768 image as a test, and have tried numerous iterations of view sizes to get it correct on my screen, but I either end up with an illogical size, or it clips.

Here is the original image; http://www.coolopticalillusions.com/backgrounds/freaky-desktop-background-7.jpg

I am trying to make the image 300 wide, which preserving aspect ratio, would mean the view should be 245 high.

This first screenshot is with a 300x245 UIWebView, you can see the width is correct, but the height is not, the red box should be aligned exactly with the bottom of the image
http://img839.imageshack.us/f/sizecropped.png/

This second screenshot is with a 300x300, which scales the image too wide for the view, and also too high as well.
http://img155.imageshack.us/f/sizetoobig.png/

I have been struggling for a while trying to get this to do what I want it to, but I am just not having any success whatsoever.

EDIT: The only relevant code I have is this:

NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"http://www.coolopticalillusions.com/backgrounds/freaky-desktop-background-7.jpg"];
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[messageView.imageWebView loadRequest:urlRequest];
messageView.imageWebView.scalesPageToFit = YES;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-11-03 14:31:37

我将首先使用应用程序框架作为框架创建网络视图。然后,您可以将图像添加到具有您的尺寸(300x245)的新图像视图中。将图像作为子视图添加到 web 视图中。

    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    webView = [[UIWebView alloc] initWithFrame:frame];

    // get image from web
    id path = @"http://www.coolopticalillusions.com/backgrounds/freaky-desktop-background-7.jpg";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data cache:NO];

    // create new image view with your prefered size
    UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
    imageView.frame = CGRectMake(0, 0, 300, 245);

    // add image view to your webview
    [webView addSubview:imageView];

让我知道这是否有帮助。

I would create the webview first with the application frame as frame. Then you can add your image to a new imageview which has your size (300x245). Add the image to the webview as subview.

    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    webView = [[UIWebView alloc] initWithFrame:frame];

    // get image from web
    id path = @"http://www.coolopticalillusions.com/backgrounds/freaky-desktop-background-7.jpg";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:data cache:NO];

    // create new image view with your prefered size
    UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
    imageView.frame = CGRectMake(0, 0, 300, 245);

    // add image view to your webview
    [webView addSubview:imageView];

Let me know if that helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文