UIImage not loading in the first instance in DetailViewController - iphone

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]

Related

IOS 5 Displaying JPEG is black

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

EGOPhotoViewer: Start at specific photo

I am using EGOPhotoViewer to load up a bunch of images from the web. They are shown in thumbnails first, so when a user clicks on one of the thumbs, I want to show that image in the EGOPhotoViewer first. So, if I clicked the 5th image, it would load the image viewer starting at 5 of 20 or whatever.
Is there a way to do this?
ZSPhoto *photo;
for (Asset *a in items) {
photo = [[ZSPhoto alloc] initWithImageURL:[NSURL URLWithString:a.imgURL] name:a.caption];
[photos addObject:photo];
[photo release];
}
// Photosource
ZSPhotoSource *photoSource = [[ZSPhotoSource alloc] initWithPhotos:[NSArray arrayWithArray:photos]];
EGOPhotoViewController *photoController = [[EGOPhotoViewController alloc] initWithPhotoSource:photoSource];
// Set starting photo index here?
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:photoController];
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
navController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:navController animated:YES];
[navController release];
[photoController release];
I was running into the same problem, What I was doing was trying to moveToPhotoAtIndex: before pushing it to the navigationController. You need to do it after.
here is how i got it to work for my application...
// gets indexPath from tableView
-(void)showSelectedPhoto:(NSIndexPath *)indexPath{
// Already had photoControllerView declared in header, init with your photo source
photoControllerView = [[EGOPhotoViewController alloc] initWithPhotoSource:source];
// pushed the photoControllerView to the navigation controller
[self.navigationController pushViewController:photoController animated:YES];
// THEN you moveToPhotoAtIndex and it should work correctly!
[photoController moveToPhotoAtIndex:indexPath.row animated:NO];
}
Hope this helps clarify things!
There is a method moveToPhotoAtIndex:animated: that you should be able to use.
i am using same process to load the images in image viewer. and app is working fine for me..you can get the code from here
selected image wouldnt load in ego image viewer in ios 7
hope you will get help.

How can I set a UIImageView's image before I present the view?

I have an app that I am working on, and in an effort to save on views that I make, I want to be able to dynamically pass the view an image. So for example, I make a view:
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
Then I want to set the image that the view's imageview shows before I present it:
UIImage *image = [UIImage imageNamed: #"IMG_5010_2.jpg"];
[controller.imageView setImage:image];
[controller.label setText:#"HI"];//I am trying to do this too and it isn't working...
But it just isn't working!! Does anyone have any thoughts on this? Please help!!
Thanks
NOTE: I do have UIImageView and UILabel attributes set on the view I am trying to present...
You should set the image within viewDidLoad method of the relevant UIViewController as the view won't exist during the init phase and will have been displayed by the time viewDidAppear is called.
Perhaps you could try this:
add two new properties to your FlipsideViewController:
#property (retain) UIImage *image;
#property (copy) NSString *labelText;
Don't forget to synthesize them in your FlipsideViewController.m.
Then when you instantiate your FlipsideViewController:
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
UIImage *image = [UIImage imageNamed: #"IMG_5010_2.jpg"];
controller.image = image;
controller.labelText = #"Hi";
and then in your FlipsideViewController viewDidLoad method you can assign the values in the properties to the view IBOutlets:
- (void)viewDidLoad {
//do other stuff
[self.imageView setImage:self.image];
[self.label setText:self.labelText];
//any other stuff
}
Sure you've got your IBOutlets hooked up properly in the nib file?
Assuming you've put this code in the right place in your view controller, this should be working fine. So it's either you're not in a place that this code is getting run, or the things you're configuring aren't hooked to any objects in the nib. It has to be one of those two things.

problems with didselectrowatindexpath

i have just converted an app i was making from a navigation controller app into a tab bar app.
an everything works fine apart from this one thing, the first of my tabs brings in a table view,and what i want to happen is when a user selects a cell i want it to push on a different view controller(like it did when it was a navigation app)
but this no longer seems to work, am i doing something silly
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
StoryDetailViewController *storyDetailViewController = [[StoryDetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
WorldCupAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
Story *aStory = [appDelegate.stories objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:aStory.picture];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
storyDetailViewController.downloadedImage = img;
storyDetailViewController.story = [appDelegate.stories objectAtIndex:indexPath.row];
[self.navigationController pushViewController:storyDetailViewController animated:NO];
NSLog(#"view controller pushed");
[StoryDetailViewController release];
}
The problem is in self.navigationController. Since it's no longer part of a navigation controller, navigationController is nil. If you want to push new views onto the hierarchy, you can do so by creating a navigation controller with that view as its root view controller, and then adding the navigation controller's view to the tab bar instead.

UIImageView not displaying in UITableView header

My problem is that I can't seem to get the image from my bundle to display properly. This method is in the view controller that controls the tableview. headerView is loaded with the tableview in the .nib file and contains a few UILabels (not shown) that load just fine. Any ideas?
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] setTableHeaderView:headerView];
NSBundle *bundle = [NSBundle mainBundle];
NSString *imagePath = [bundle pathForResource:#"awesome_lolcat" ofType:#"jpeg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
imageView = [[UIImageView alloc] initWithImage:image];
}
If you've already created an outlet and connected it to a view in Interface Builder, you should use that view, rather than creating a UIImageView on the fly.
//this assumes that headerView is an already created UIView, perhaps an IBOutlet
//also, imageViewOutlet is an IB outlet hooked up to a UIImageView, and added as
//a subview of headerView.
//you can also set the image directly from within IB
imageViewOutlet.image = [UIImage imageNamed: #"awesome_lolcat.jpeg"];
[[self view] setTableHeaderView: headerView];
FIrst you need to figure out whether your image is loading properly. The quickest way to get an image is to use the UIImage convenience method +[UIImage imageNamed: rawImageName].
Also, is this a UITableViewController? (it's unclear, but implied).
Where is imageView being used? You create it near the bottom, but don't seem to do anything with it. You probably want to create the image view, assign it an image, and then add it as a subview to the headerView.
//this assumes that headerView is an already created UIView, perhaps an IBOutlet
UIImage *image = [UIImage imageNamed: #"awesome_lolcat.jpeg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[headerView addSubview: [imageView autorelease]];
[[self view] setTableHeaderView: headerView];
Adding the subview programatically worked, but it isn't correctly linked to the UIImageView in Interface Builder. The view I created is (I think) correctly linked to the UIView outlet in my UITableViewController, but when I init my imageView it creates a new view instead of putting the image in the view I already created.
Thanks for your answer.
See http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html
It will display UIImageview on section header.