Load UIImage form NSData in UIWebView and center it - iphone

So I basically get an NSData of UIImage and I use a UIWebView to display it. I do it that way:
[self.view addSubview:webView];
webView.delegate = self;
webView.scalesPageToFit = YES;
webView.backgroundColor = [UIColor darkGrayColor];
I then load the picture into it like this:
NSData *image02Data = attachmentToLoad.attachment_data;
[webView loadData:image02Data MIMEType:#"image/jpg" textEncodingName:nil baseURL:nil];
And I get a result that isn't exactly what I want:
As you can see the image leaves a white bottom line down at the bottom of the UIWebView. How can I make the size of the UIWebView to be equal to that of the image?

Have you seen this post?
Click me
I think it is exactly you want

Related

Centering UIWebView content with padding

I have a UIView which loads a html5 banner. I get padding on each side of the banner which is to be expected since the banners width is shorter than the UIWebView. However.. The padding on each side is a lot bigger than I should get.
I just want the banner to be centered if the banner is shorter than the UIWebView.
Instead of looking like this: (Where X is banner and O is padding)
|OOXXXXXXOO|
It looks like this:
|OOOOXXXXXX|
Here, if I enable scrolling I can center it like the first example.
How do I fix this?
Code
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:bannerIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"bannerCell"];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.bounds.size.width, cell.contentView.bounds.size.height)];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
webView.userInteractionEnabled = YES;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.delegate = self;
webView.scrollView.bounces = NO;
webView.scrollView.scrollEnabled = NO;
webView.scalesPageToFit = YES;
NSURL *url = [NSURL URLWithString:#"http://example.com/page.html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[cell.contentView addSubview:webView];
}
The thing is UIWebView shows HTML, that means, that if your HTML has to be rendered as you wish - the only correct way to achieve this is to fix loaded HTML (and or css).
You can do it in several ways:
If you have access to the HTML data on server you could tweak it to be rendered correctly in UIWebView.
If you don't have access to the HTML data on server you could use stringByEvaluatingJavaScriptFromString: and fix desired elements in DOM.
You can use stringByEvaluatingJavaScriptFromString: to query the DOM for the width of the desired element using Javascript. You can then resize the webview to fit that size.
This gives you the width:
document.getElementById('YOURID').offsetWidth;

UIWebview scalesPageToFit doesn't work perfectly

I'm using a small UIWebView, and scalesPageToFit doesn't work correctly for some url. (look like it cannot resize at 100%. (but if i zoom out the webview manually(gesture), it work!
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 400.0f)];
webViewT = [[UIWebView alloc] initWithFrame:view.bounds];
webViewT.delegate = self;
webViewT.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:#"http://www.google.com/"];
[webViewT loadRequest:[NSURLRequest requestWithURL:url]];
Please note that i've found a solution:(below work, BUT I have the time to see a "resize"
- (void) webViewDidFinishLoad:(UIWebView *)webViewT {
if ([theWebView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[theWebView scrollView];
float zoom=theWebView.bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:NO];
}
}
Any idea? Thanks!
In Some of cases this types of problem Generated. At that time java script use for control of Zoom of UIWebView, use following code.
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Other way is
Refer this Answer UIWebView does not scale content to fit

Adding animation at start up using an array of images

I'm new to iphone app development and I'm trying to add an image to the start of my app. I've created an array of images which when iterated should create an animation.
-(void)viewDidLoad
{
animation = [[UIImageView alloc] init];
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"panda1.png"],
[UIImage imageNamed:#"panda2.png"],
[UIImage imageNamed:#"panda3.png"], nil];
animation.animationRepeatCount = 2;
animation.animationDuration = 2;
[animation startAnimating];
[self.view addSubview:animation];
[animation release];
//for loading the webpage at start up
NSString *urlAddress = #"http://projects.seng.uvic.ca/fatpanda/m/most_recent.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//load the request in the UIWebView
[webView loadRequest:requestObj];
}
The reason I release the animation at the end of this is because I'd like to remove the last image so I can replace it with a view of a website in a UIWebView and have that visible at the end. The code above compiles but when I run it no images appear but the website does.
Any help would be greatly appreciated.
try this, i think your frame is ending up all zeros.
UIImage * first = [UIImage imageNamed:#"panda1.png"]
animation = [[UIImageView alloc] initWithImage:first];
animation.animationImages = [NSArray arrayWithObjects:
first,
[UIImage imageNamed:#"panda2.png"],
[UIImage imageNamed:#"panda3.png"], nil];
Also...
releasing the animaiton will not remove it from the superview. in fact calling addSubview will increase the reference count to the UIImageView and releasing it is the correct thing to do. When you want to to remove from the view it you will have to find it again in the list of subviews and either hide it or call removeFromSuperview.

How make webview with Rounded Rectangle corners?

I want my webview with Rounded Rectangle Corners.
Any help ?
Here's how:
//first, you
#import <QuartzCore/QuartzCore.h>
//.....
//In your method, where you add your UIWebView, do:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
//The rounded corner part:
webView.layer.cornerRadius = 5;
webView.clipsToBounds = YES;
//Load a web site:
[webView loadRequest: [NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://www.stackoverflow.com/"]]];
//yourView is the UIView's superview, might be the window, or anything you want
[yourView addSubview: webView];
[webView release];
This uses the QuartzCore framework, and it only works on >= OS 3.0
you can add some mask image over your webView. in such way you can change form of the visible part of your webView

Anti-aliasing not working when resizing a UIWebView

I'd like to add a Web View to my app at 60% scale (like seen in Safari in the browse other windows view):
Notice how the content looks nice and Aliased!
If I try and add the same Web view to my app:
NSURL *url = [NSURL URLWithString:#"http://www.google.co.uk?q=hello"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
webView.delegate=self;
[webView loadRequest:request];
[self.view addSubview:webView];
Using the following transformation:
[webView setTransform:CGAffineTransformMakeScale(0.6, 0.6)];
..the scale is really bad quality and there appears to be no anti-aliasing.
Does anyone know why this is happening or have a suggestion on how it could be fixed?
Thanks!
Nick.
I figured out a solution!
Take a snapshot of the webview, add it to a UIImageView and resize that insted!
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(webView.bounds.x, webView.bounds.y, webView.frame.size.width, webView.size.height)];
imageView.image = viewImage;
[self addSubview:imageView];
[imageView release];
webView.hidden = YES;
Seems to work fine!
One point to note.. if you want to do this as soon as the WebView has loaded, you need to defer the action by some period (I chose a 10th of a second!)
Like so:
-(void)deferLoading
{
webViewHasActuallyLoaded = YES;
[self setNeedsLayout];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[NSTimer scheduledTimerWithTimeInterval: 1.0/10.0 target:self selector:#selector(deferLoading) userInfo:nil repeats:NO];
}
Cheers for suggestions abovee!
Nick.
Why not just make the web view smaller? It should render the content in high quality at the smaller size.
If you need to animate the change in size, you can set the minificationFilter on the web view's layer to get reasonable quality:
[[webView layer] setMinificationFilter:kCAFilterLinear];
In your UIViewController override
(void)placeViewItems: (UIInterfaceOrientation)interfaceOrientation
And make your UIWebView's frame.origin.x & frame.origin.y integer values.
See if that is the result you are looking for.