Add PDF or PPT file on UIScrollview in iPhone - iphone

I am working on display PDF from resourse folder
on UIScrollview. But it is not able to add.
I add UIScrollView on ViewController. Then take a UIImageView and add on UIScrollView as bellow code.
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0,768,1024)];
scrollView.contentSize = CGSizeMake(768,2000);
scrollView.delegate = self;
scrollView.pagingEnabled = NO;
[self.view addSubview:scrollView];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(00, 20, 500,900)];
NSString *url = [[NSBundle mainBundle]pathForResource:#"Acknowledgements" ofType:#"pdf"];
img.image=[UIImage imageWithContentsOfFile:url];
[scrollView addSubview:img];
But pdf is not added on scrollview. If i add some image file like
NSString *url = [[NSBundle mainBundle]pathForResource:#"trophy" ofType:#"png"];
It get added.
PDF file is correct and open in Mac. Same case happen with PPT file.
what is going wrong.
Thanks

#interface UIWebView : UIView < NSCoding, UIScrollViewDelegate > {
UIWebView implements UIScrollViewDelegate. So you can still handle your scroll related events.
Check this link for delegate methods of UIScrollView http://developer.apple.com/library/ios/#documentation/uikit/reference/uiscrollviewdelegate_protocol/Reference/UIScrollViewDelegate.html

ppt/pdf cant be added to UIImageView, rather than add it in UIWebView
NSString *url = [[NSBundle mainBundle]pathForResource:#"Acknowledgements" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:url];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

Related

Memory management when adding a resized UIImage to a UITableViewCell

I have images cached and I want to resize them and display them in a tableview. Here's the code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://huntify.com%#",[[hunts objectAtIndex:indexPath.row] iconURL]]];
UIImage *logo = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]];
logo = [logo imageScaledToSize:CGSizeMake(100.0, 100.0)];
[[cell imageView] setImage:logo];
This works fine but I wonder how I should release the UIImage. I suspect that releasing the "logo" after setting the image-property to the UIImageView is wrong because of the imageScaledToSize-method. Should I assign a new UIImage when resizing and release the old one?
You have a big memory leak here. By assigning another value to logo in logo = [logo imageScaledToSize:CGSizeMake(100.0, 100.0)];, you lose the reference to the original image you would need to release. So yes, you should use a new variable to store the result of imageScaledToSize: and then release the original logo.
Do this-
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://huntify.com%#",[[hunts objectAtIndex:indexPath.row] iconURL]]];
UIImage *logo = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]];
UIImage *resizedLogo = [logo imageScaledToSize:CGSizeMake(100.0, 100.0)];
[[cell imageView] setImage:resizedLogo];
[logo release];
Remember, you are only releasing owenership of an object when you [logo release], you are not dealloc'ing it.

Iphone secondary xib, UIWebview local htm file will not load. I have tried numerous methods nothing has worked

I am loading as secondary view with its own xib file. I can load it fine, but cannot get the webview in it to load any content. I have another app where this proces works, but it is in the main view.
Here is the code I am using.
#synthesize webView;
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"workshop" ofType:#"htm" inDirectory:#"dirA"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData:
[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
// to make html content transparent to its parent view -
// 1) set the webview's backgroundColor property to [UIColor clearColor]
// 2) use the content in the html: <body style="background-color: transparent">
// 3) opaque property set to NO
//
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
[self.webView loadHTMLString:htmlString baseURL:nil];
[htmlString release];
}
- (void)dealloc
{
[webView release];
[super dealloc];
}
-(IBAction)switchCanary {
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction)switchBack {
[self dismissModalViewControllerAnimated:YES];
}
#end
I figured it out. For some reason I was able to load .htm files before. I had to change them all to .html and now it works. Maybe a recent change. The other app was a couple SDK versions ago.

PDFs not showing, Other URLs do

I am having difficulty with a UIWebView which shows http URLs correctly, but not bundled .pdf files.
I have also read alternative solutions such as using QuartzCore framework, however this does not seem to support multiple pages in PDFs.
Here is the code that works (for google dot com)
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"file" ofType:#".pdf"];
NSURL *pdfURL = [NSURL URLWithString:pdfPath];
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y += kTopMargin - 20.0; // leave from the URL input field and its label
webFrame.size.height -= 00.0;
self.webView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.scalesPageToFit = YES;
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.delegate = self;
[self.view addSubview: self.webView];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
}
And for the PDF, I have replaced
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
with
[self.webView loadRequest:[NSURLRequest requestWithURL:pdfURL]];
My immediate thought was that the iPhone SDK does not support PDF viewing in its webview, however, tutorials like this one seem to show other users having success with this. So do you think I am just missing something in my code?
Thanks.
UIWebView does support pdf when using loadRequest as you are doing.
pathForResource:#"file" ofType:#".pdf"
Take the . out of .pdf
Edit:
[NSURL URLWithString:pdfPath];
Use fileURLWithPath: instead.
Check that pdfPath and pdfURL are what you expect them to be.
NSLog( #"%#" , pdfURL );

UIWebView not loading URL when URL is passed from UITableView

So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.
here's the code in my UIViewController that has my UIWebView
#import <UIKit/UIKit.h>
#interface ReadFeedWebViewController : UIViewController
{
NSString *urlToGet;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain) IBOutlet UIWebView *webView;
#property(nonatomic, retain) NSString *urlToGet;
#end
Here's the code for my implementation's viewDidLoad method...
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Url inside Web View Controller - %#", urlToGet);
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:#"link"];
//NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"summary"]);
NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"link"]);
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers
The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished.
I should you to call it in the viewWillAppear method like that:
- (void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Also, there is a memory leak in your code. You should release extendedView after pushing it on the navigation controller.
ReadFeedWebViewController *extendedView = [ReadFeedWebViewController new];
if (extendedView != nil) {
... setup extendedView here ...
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
Try this
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];

Trouble loading html file from plist to webView on iPhone

trouble loading html file from plist to webView using following code in
FAQDetailViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *WebPath = [Path stringByAppendingPathComponent:WebFile];
UIWebView *tempWeb = [[UIWebView alloc] initWithContentsOfFile:WebPath];
[webView loadData:tempWeb]; //I think my issue is here. I am not understanding how to implement the the code here
[tempWeb release];
}
loaded here with this peace of code in FAQViewController.m:
FAQDetailViewController *faqdvController = [[FAQDetailViewController alloc] initWithNibName:#"FAQDetailView" bundle:[NSBundle mainBundle]];
faqdvController.WebFile = [dictionary objectForKey:#"faqDesc"]; //html file from plist file placed here
faqdvController.hidesBottomBarWhenPushed = NO;
[self.navigationController pushViewController:faqdvController animated:YES];
[faqdvController release];
You can try it this way:
NSString *bundle = [[NSBundle mainBundle] bundlePath];
NSString *webPath = [bundle stringByAppendingPathComponent:{htmlFilename}]
UIWebView* browser = [[UIWebView alloc] initWithFrame:
CGRectMake(0, 0, 200, 300)];
[browser loadRequest:[NSURLRequest requestWithURL:
[NSURL fileURLWithPath:webPath]]];
[self addSubview:browser];
You'll also want to set delegates on the browser so you get informed when the contents have finished loading.