IOS 5 Displaying JPEG is black - ios5

I have a TableView, when I click on a cell it pushes my class ImageViewController and displays an image. This is working great. The viewDidLoad method is here.
- (void)viewDidLoad
{
NSLog(#"ImageViewController Load");
NSLog(#"Image String: %#",imageString);
self.view.contentMode=UIViewContentModeCenter;
UIImage *im = [UIImage imageNamed:imageString];
if(im == nil){
NSLog(#"Image is NULL");
}
[image setImage:im];
self.title = #"View Image";
[super viewDidLoad];
}
imageString is defined as an NSString in the .h file. I'm using a StoryBoard and launch my ImageViewController by:
ImageViewController *imageViewController = [segue destinationViewController];
imageViewController.imageString = [[listData objectAtIndex:selectedIndex]objectForKey:#"image"];
I'm experience problems (the screen is black) when I try and view an image when clicking on a link within a UIWebView. Basically here the UIWebView contains all my content and I'd like to view an image when cliking on a link.
To achieve this I'm using the following method:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
And launch the ImageViewController using the following javascript
window.location.href="eft://?type=image&path=Google.jpg";
Within the shouldStartLoadingWithRequest method I push the ImageViewController using the following:
ImageViewController *imageViewController =[[ImageViewController alloc]init];
imageViewController.imageString = path;
[self.navigationController pushViewController:imageViewController animated:YES];
The image isn't null as I perform a check and the path and type fields are correct but the screen is black. Rather than rely on the path and type fields I manually set these fields in the ImageViewController (for exmaple google.jpg) and it still shows a black screen when loading the controller from the UIWebView. If I load it from the table view it works perfectly???
Thanks in advance

I've found the answer, i need to init with storyboard reference

Related

QLPreviewController NavigationBar setTranslucent property not working

i have set the custom color to navigation bar of QLPreviewController
but the problem is that i want dark color of navigation bar in QLPreviewController
even i have set the navigation bar translucent property to No
But i Dont know Why it is not working
I want it like belowed image
but it shows like this image
QLPreviewController *previewer = [[QLPreviewController alloc] init];
// Set data source
[previewer setDataSource:self];
[previewer setDelegate:self];
// Which item to preview
[previewer setCurrentPreviewItemIndex:index];
[previewer.view addGestureRecognizer:singleTap];
previewer.navigationController.navigationBar.translucent = NO;
previewer.navigationController.navigationBar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.translucent=NO;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
[self.navigationController pushViewController:previewer animated:TRUE ];
even i have also tried like this but it is also not working
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
// Break the path into it's components (filename and extension)
// Use the filename (index 0) and the extension (index 1) to get path
//lblFileName.text=[strFileName stringByReplacingOccurrencesOfString:#"movefile" withString:#""];
// For navigation bar color and text attributes of navigation bar
for (id object in controller.childViewControllers)
{
if ([object isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = object;
navController.navigationBar.translucent=NO;
navController.navigationBar.barTintColor = [UIColor redColor];;
navController.toolbar.translucent=NO;
navController.toolbar.barTintColor=[UIColor redColor];;
[navController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
}
}
NSString *strFilename=[[NSBundle mainBundle]pathForResource:#"final" ofType:#"png"];
return [NSURL fileURLWithPath:strFilename];
}
Please suggest me where i am going wrong
Thanks In Advance
The main problem is that when you try to set the translucency of the navigation bar, you haven't pushed the preview controller on the navigation stack yet.
At this point, the preview controller is allocated and instantiated, but its view has not been loaded or added to the view hierarchy, and the value of previewer.navigationController is nil. The value of self.navigationController is not nil at this point, but the translucency property you set here will be overwritten as a side effect of pushing the preview controller. The easiest way to get the effect you want, is to swap the order of the statements, like this:
[self.navigationController pushViewController:previewer animated:YES];
self.navigationController.navigationBar.translucent = NO;
Do note that with the translucency of the navigation bar set to NO, the previewed content will start under the navigation bar, which is probably not what you want. The easiest way to work around that issue, is to set the translucency property after the view of the preview controller has appeared on screen. You can do this by subclassing QLPreviewController:
#interface PreviewController : QLPreviewController
#end
#implementation PreviewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.navigationController.navigationBar.translucent = NO;
}
Note that things get more complicated when you present the preview controller modally (rather than pushing it on a navigation stack). In that case, there is no navigation controller available to access the navigation bar and you need to rely on the internal view hierarchy of the QLPreviewController. The following code works in iOS7, but might break in a later version:
[self presentViewController:previewController animated:YES completion:^{
UIView *view = [[[previewController.view.subviews lastObject] subviews] lastObject];
if ([view isKindOfClass:[UINavigationBar class]])
{
((UINavigationBar *)view).translucent = NO;
}
}];
No need for subclassing. try this:
QLPreviewController * ql = [[QLPreviewController alloc] init];
...
[self showViewController:ql sender:self];
ql.navigationController.navigationBar.translucent = NO;
ql.edgesForExtendedLayout = UIRectEdgeNone;
If you want to present it modally, just put the previewController into a navigationController:
UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:ql];
ql.navigationController.navigationBar.translucent = NO;
ql.edgesForExtendedLayout = UIRectEdgeNone;
[self presentViewController:navi animated:YES completion:nil];
Instead of using QLPreviewController,y don't you use webview to load the pdf.But if you load pdf in webview you need to customise the buttons thats it.Its also look very similar to QLPreviewController.
NSURL *targetURL = [NSURL fileURLWithPath:path_pdf]; //path_pdf is pdf's path
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webview_pdf loadRequest:request];
Well,try it once.
If Still, anyone wants to customize the Navigation bar of QLPreviewController then they can try creating a UIViewController and customize it as per requirement and then create QLPreviewController object and add it as a child view controller.
This will allow you to get rid of share button and you customize navigation bar color etc. this is working for me.
To know, how to add child view controller you can refer to this

iOS Storing and Loading Multiple web views in and from an array

My app used to use a single UIWebView from the storyboard to display a users online data.
Now I have added the ability to have multiple accounts stored in the app, and so I would like to be able to have a separate UIWebView for each account, and show the appropriate one.
My app is a tabbed application, with one tab holding the account details in a table view, and the other with the UIWebView. This is what I have tried so far to have multiple web views.
In the table view controller, my didSelectRowAtIndexPath looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Generate the url (please note I've taken this code out)
...
//Get the view controller that will hold the web view
NSArray *viewControllers = self.tabBarController.viewControllers;
ViewerFirstViewController *viewerViewController = [viewControllers objectAtIndex:0];
//Look for the web view in an array of web views
[viewerViewController getWebView:indexPath.row];
[viewerViewController setViewerUrl:viewerURL];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Move to the tab that will hold the web view (bonus question - how can I animate this move?
[self.tabBarController setSelectedIndex:0];
}
Now, for the view controller that will hold the web view
-(void)getWebView:(int)index
{
if ([webViews objectAtIndex:index] == NULL) {
[self createWebView:index];
[self setDidExist:false];
[self setWebView:[webViews objectAtIndex:index]];
} else {
[self setDidExist:true];
[self setWebView:[webViews objectAtIndex:index]];
}
}
-(void)createWebView(int)index
{
float width = self.view.bounds.size.width;
float height = self.view.bounds.size.height;
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
wv.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
wv.scalesPageToFit = YES;
wv.delegate = self;
[self.view addSubview:wv];
[webViews insertObject:wv atIndex:index];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewDidLoad];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerUrl];
if (didExist) {
[webView loadRequest:requestObj];
[webView setHidden:false];
} else {
[webView loadRequest:requestObj];
}
}
It appears that the web view is being created as I am presented with a white screen that I can scroll in, but no page is being loaded. I'm assuming this won't work to hide the web views not currently in use either.
The web view should only load the page if it didn't exist previously. If it did already exist then it should present the loaded web view that is stored in the array.
Am I loading the URL at the wrong time or something? I'm unsure as to why I get a web view created but it's not filled with anything..
What seems to be happening is the web view isn't being added to the array when I create it - the array is empty and so webView isn't being set and so the url is never loaded into it However the code I use to add it to the array seems ok to me..
[webViews insertObject:wv atIndex:index];
I changed it from storing them in an NSMutableArray to an NSMutable Dictionary. This solved my problems.

UIImage not loading in the first instance in DetailViewController

I have a master-detail application. I list all images from the application documents folder in the MasterViewController tableview. On the click of each tableview item, I load the image in the DetailViewController, which has a UIImageView. The problem I am facing is that, the images does not load in the first instance, when I click a tableview item. I just see the message "The detail view content goes here", in the DetailViewController. If I go back and try again, it will work just fine. Its just the first time that the image wont load.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[PtmDetailViewController alloc] initWithNibName:#"PtmDetailViewController" bundle:nil];
}
//NSDate *object = _objects[indexPath.row];
NSData *imgData = [NSData dataWithContentsOfFile:self.fPath];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
self.detailViewController.imgViewer.image = thumbNail;
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
What am I doing wrong? Any help would be appreciated.
It seems like the view of detailViewController doesn't have time to load. So you are setting the image to the nil UIImageView (it's not initialized yet).
Try adding a property UIImage *thumb to detailViewController and then in detailViewController's viewDidLoad add
imgViewer.image=self.thumb;
And in your tableView:didSelectRowAtIndexPath: change
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
self.detailViewController.imgViewer.image = thumbNail;
to
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
self.detailViewController.thumb = thumbNail;
I guess, first time either self.fpath is coming nil or there is no image in that path. Check the imgData is having valur or nil at first time. Then show your image,
[UIImage imageWithContentsOfFile:self.fpath]

How to set UINavigationController Image for only one view and then another image for the rest of the views?

I am trying to set the background image for my navigation controller. On the very first view i want to display a different image and then same in every other view.
Currently what i got is i am able to display the image on first view but somehow i am not able to remove it and its overlapping with other image on the next view.
Here's the code:
- (void)viewDidLoad {
[super viewDidLoad];
navImage = [[UIImageView alloc]init];
navImage.image = [UIImage imageNamed:#"iphone_dashboardFirst.png"];
WBWAppDelegate * appDelegate = [(WBWAppDelegate*)[UIApplication sharedApplication]delegate];
appDelegate.rootNav.navigationItem.titleView = navImage;
}
-(void)viewWillDisappear:(BOOL)animated
{
navImage.image = [UIImage imageNamed:#"iphone_dashboard_header.png"];
WBWAppDelegate * appDelegate = [(WBWAppDelegate*)[UIApplication sharedApplication]delegate];
appDelegate.rootNav.navigationItem.titleView = navImage;
}
The easyest way to do this is to do the same thing you did here in the viewDidLoad: in the viewDidLoad: of the next controller. The viewWillDisappear: has caused me some trouble before aswell, I'm not really sure why.
EDIT
You could check if any of the following methods work:
– viewWillDisappear:
– viewDidDisappear:
– viewDidUnload
These are the only methods I could think of that might work.

adding a background image to UITabBarController

This is what I have from googling, but evidently it's not what I need:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// add the tab bar controller to the window
[window addSubview:[tabBarController view]];
// load the image, create a view with the image
NSString* dirPath = [[NSBundle mainBundle] bundlePath];
NSString* imageFile = [dirPath stringByAppendingString:#"homebackground.png"];
UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed:imageFile]];
[[tabBarController tabBar] addSubview: view];
}
Yes, the image file loads fine..I think what the above does is attempt to change the bg of the navigation bar area of the tab bar? I'm not certain.
Does anyone know how I would go about changing the actual background with an image?
Thank you!
Try adding the image as the lowest layer explicitly via insertSubview:atIndex: using index 0.
Edit: There's an explicit setBackgroundImage category for UITabController in this thread:
Share background view between tabs on a UITabBarController