fill UIWebView with local "loading" image before internet content - delegates

I have a UITableView with header as a uiwebview (320*44px) for pub content. I wonder how to show some local content image or a background color or whatever before the content get loaded from the web ??
(goal: eleminate the white default background for not-loaded uiwebview banner)

//i assume ur webview object name as webView;
//i assume u were use delegate to ur webview by specifing after creating the webview like
webView.delegate=self;
//dont forget to in .h file #interface urViewController : vijayviewController<UIWebViewDelegate> { <UIWebViewDelegate> is matter.
UIImageView *imageView=[[UIImageView alloc]initWithFrame:webView.bounds];
[imageView setImage:[UIImage imageNamed:#"vijay.png"]];//image size should ur specification 320*44px
[imageView setTag:55];
[webView addSubview:imageView];
[imageView release];
//to remove image
// [[webView viewWithTag:55] removeFromSuperview];
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[[webView viewWithTag:55] removeFromSuperview];
}

Related

Resize UIImageView in TableViewCell

I have UIImageView in my TableViewCell. This Image parse from for example apple JSON. Image show and all work perfectly but i have problem with resize this image in cell. I tried this but nothing. I read many topics and peoples have the same problem. I know how to make size for picture from URL but i have parse image and paste to cell.imageView.
So my code of image from JSON:
[cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:#"artworkUrl60"]] placeholderImage:[UIImage imageNamed:#"noholder.png"]];
And this code i tested but nothing to change in size of picture in cell
cell.imageView.frame=CGRectMake(50,50,300,300);
How to change the size of image in cell but without URL link. I must change size picture from cell.Thanks.
You'll need to create a custom cell (at least for iOS 7).
Here's a sample:
.h
#interface UITableViewImageCell : UITableViewCell {
CGRect _imageViewFrame;
}
/**
* To use default, set width and height to 0
*/
#property(nonatomic, assign) CGRect imageViewFrame;
#end
.m
#implementation UITableViewImageCell
#synthesize imageViewFrame=_imageViewFrame;
-(void)setImageViewFrame:(CGRect)imageViewFrame{
_imageViewFrame = imageViewFrame;
[self setNeedsLayout];
}
-(void)layoutSubviews{
[super layoutSubviews];
if (_imageViewFrame.size.width>0 && _imageViewFrame.size.height>0) {
self.imageView.frame = _imageViewFrame;
}
}
#end
Using this class, you'd have to call:
cell.imageViewFrame=CGRectMake(50,50,300,300);

XCode Prevent UIWebView flashing white screen after Launch Image

I am fairly new to XCode - I'm designing an app with a single view that just displays a UIWebView that loads my jQuery mobile web site.
I have the Default.png and Default#2x.png files for the Launch images in place. When I run the app on my test device or in the emulator, it shows my Launch Image, and then flashes a white screen while the UIWebView loads the first page.
I know that I can change the background color and opacity so it will flash a different color than white, but I would like to prevent the flash altogether. I would like the app to launch, show the Launch image, and not show the UIWebView until the first page has completely loaded. Help?
# Andreas Volz (and anyone else wondering how I solved the problem of the white "flash")
First, add the name-of-loading-image.png and name-of-loading-image#2x.png files to your Xcode project. The regular image should be 320 x 460, and the #2x image should be 640 x 920.
Then, in my ViewController.h file I added these properties:
#property (nonatomic, retain) UIWebView *webView;
#property(nonatomic, strong) UIImageView *loadingImageView;
#end
And then in my ViewController.m file I added this:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
#synthesize webView;
#synthesize loadingImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
//**************** Set website URL for UIWebView
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com"] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0]];
//**************** Add Static loading image to prevent white "flash" ****************/
UIImage *loadingImage = [UIImage imageNamed:#"name-of-loading-image.png"];
loadingImageView = [[UIImageView alloc] initWithImage:loadingImage];
loadingImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"name-of-loading-image.png"],
nil];
[self.view addSubview:loadingImageView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Remove loading image from view
[loadingImageView removeFromSuperview];
}
To know when your webView has finished loading, you must implement the UIWebViewDelegate protocol.
In your viewController's .h file:
#interface viewController : UIViewController <UIWebViewDelegate>
{
UIWebView *webView;
}
#end
Then in your viewController's .m file, add this method:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"content loading finished");
// Display your webView
[self.view addSubview:webView];
}
You can use the -viewDidLoad method in your viewController's .m to continue displaying your launch image. You can also begin the loading of content for your webView:
- (void)viewDidLoad {
[super viewDidLoad];
// continue to display launch image
UIImage *launchImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"launchImage.png" ofType:nil ]];
UIImageView *launchImageView = [[[UIImageView alloc] initWithImage:launchImage] autorelease];
[self.view addSubview:launchImageView];
// now setup the base view for the webView controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];
// for rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
//create a frame to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
self.webView = aWebView;
//aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//set the web view delegates for the web view to be itself
[aWebView setDelegate:self];
//determine the path the to the index.html file in the Resources directory
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
}

UIImagePickerController within existing UIPopoverController resizing issue on iPad

I am working on an iPad app where I have a button that generates a popover controller that I am populating with a specific view controller. Here is the button code that generates the UIPopoverController and loads it with a specific view (signUpListAddEditViewController):
-(void)addSignUpList:(id)sender {
signUpListAddEditViewController = [[SignUpListAddEditViewController alloc] init];
popover = [[UIPopoverController alloc] initWithContentViewController:signUpListAddEditViewController];
popover.popoverContentSize = signUpListAddEditViewController.view.frame.size;
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
signUpListAddEditViewController.popover = popover;
}
Inside SignUpListAddEditViewController.m, I have several fields and a tableview. One of the fields is a UIButton where I display an image and allow the user to load a new image using this button. Since I am already in a popover, I swap the content controller with the UIImagePickerController. So far, so good.
-(void)takePicture:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[popover setPopoverContentSize:popupScreenSize animated:YES]; // popupScreenSize is a CGSIZE set to 500 x 900
[popover setContentViewController:imagePicker animated:YES];
}
The view is swapped out with the imagePicker with the size remaining the same as the original view and I can see the various picture galleries (Camera Roll, Photo Stream, etc). Here is where it gets strange. As soon as I pick one of the galleries, the view is replaced with a thumbnail view of the pictures in that gallery AND THEN the view resizes to a much narrower width. The images are distorted and nearly impossible to select one of them.
I have tried setting [popover setPopoverContentSize:CGSIZE animated:YES] prior to every access to the popover controller, with everything resizing properly EXCEPT the detailed image view? Any ideas where I can override the thumbnail image gallery view?
Here is the code for the didFinishPickingMediaWithInfo method where I again override the content size:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[thisList setThumbnailDataFromImage:image];
[popover setPopoverContentSize:popupScreenSize animated:YES];
[popover setContentViewController:self animated:YES];
listImageButton.imageView.image = image;
}

cocoa: How to make background transparent webView?

The mac osx ,I want to make my webview to look transparent and show the data on it with background showing images of parent view.
Can anyone please let me know how to do this?
This method can not
NSString * source = #"<html><head><title></title></head><body><div id=\"box\">Example Box</div></body></html>";
[[webView mainFrame] loadHTMLString:source baseURL:[NSURL URLWithString:#""] ];
[[webView windowScriptObject] evaluateWebScript:#"document.getElementById('box').style.backgroundColor = '#dddddd';"]; // Change box background
[[webView windowScriptObject] evaluateWebScript:#"document.body.style.backgroundColor = '#dddddd';"]; // Change body background
You need to call setDrawsBackground: on the WebView and pass in NO. That will prevent the web view from drawing a background unless the page loaded in the web view explicitly sets the background color.
We can add background image in WebView using cocoa. to do this we need to add css for body tag.
Create a webview:
WebView *webView;
Fetch image form bundle path:
NSString *path = [[NSBundle mainBundle] pathForResource:#"my-bg" ofType:#"jpg"];
write Css for body tag.
NSMutableString *htmlBuilder = [[NSMutableString alloc] init];
[htmlBuilder appendString:#"body {"];
[htmlBuilder appendString:[NSString stringWithFormat:#" background-image: url('file://%#');",path]];
[htmlBuilder appendString:#"}"];
Load this image on webview
[[webView mainFrame] loadHTMLString:htmlBuilder baseURL:nil];
[webView setDrawsBackground:NO];
Your image will be added in background on webview.

How do I put a background image on a text area (IN THE IPAD)

How do I put a template'd background image on a text area?
Not sure what you mean with "template'd"... but....
In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.
You can set the contents property of the text views layer.
#import <QuartzCore/QuartzCore.h>
...
- (void) viewDidLoad {
[super viewDidLoad];
textView.layer.contents = (id)[UIImage imageNamed:#"myImage.png"].CGImage
}
should work
If you want to add the image programmaticaly I managed to do it this way:
First in my textviewViewController.h I added an outlet for the UITextView.
#import <UIKit/UIKit.h>
#interface textviewViewController : UIViewController {
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end
Next, in my .xib I added my UITextView and connected my outlet.
Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Change the pathForResource to reflect the name and type of your image file
NSString *path = [[NSBundle mainBundle] pathForResource:#"d" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.textView insertSubview:imgView atIndex:0];
[imgView release];
}
I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.

Resources