Having trouble passing NSString from plist to ImageView - ios

I'm currently using a plist to populate my table view rows(i.e. Name, Description and Image). When the user selects a row, a new controller is pushed up with an imageView presenting the rows image in full screen.
The problem that I'm facing is, passing the string to the new viewController's imageView.
All the NSLog's return the correct information, except that when it logs the UIImageView, it returns null then. Am I not connecting it correctly? The row doesn't display any image until it's selected (essentially the row is acting, similar to a thumbnail).
Any help would be greatly appreciated thank you!!!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *childDictionary = [mainChildren objectAtIndex:indexPath.row];
//Image Name NSString from plist
childImage = [childDictionary objectForKey:#"Child Image"];
if ([childDictionary objectForKey:#"Child Image"] == nil) {
NSLog(#"No Image String Found.");
}
else {
NSLog(#"Image String Found. Image Name is: %#", childImage);
UIImage *myImage = [UIImage imageNamed:childImage];
NSLog(#"Image Found. Image is: %#", myImage);
UIImageView *childImageView = [childImageView setImage:myImage];
NSLog(#"ImageView Found. ImageView is: %#", childImageView);
FullscreenImageViewController *imgViewer = [[FullscreenImageViewController alloc] init];
imgViewer.fullScreenImageView = childImageView;
[self presentViewController:imgViewer animated:YES completion:nil];
}
}

When you instantiate a new UIImageView, it should be [[UIImageView alloc] initWithImage:image].
Or since your full screen image controller has property of image view, e.g. fullScreenImageView, you can just set the image of the property directly with a UIImage instance.

Related

Show multiple images in an Image View in IOS

I'm using ELCImagePickerController in my project. Select multiple images from the gallery through it. I'm facing an issue that , how i can show the selected multiple images in an image view in a view controller. I have created a select image button which take us to photolibrary where we select multiple images.My code is,
- (IBAction)Select:(id)sender {
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
elcPicker.maximumImagesCount = 100; //Set the maximum number of images to select to 100
elcPicker.returnsOriginalImage = YES; //Only return the fullScreenImage, not the fullResolutionImage
elcPicker.returnsImage = YES; //Return UIimage if YES. If NO, only return asset location information
elcPicker.onOrder = YES; //For multiple image selection, display and return order of selected images
// elcPicker.mediaTypes = #[(NSString *)kUTTypeImage, (NSString
*)kUTTypeMovie]; //Supports image and movie types
elcPicker.imagePickerDelegate = self;
[self presentViewController:elcPicker animated:YES completion:nil];
}
- (void)elcImagePickerController:(ELCImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
for (NSDictionary *dict in info)
{
if ([dict objectForKey:UIImagePickerControllerMediaType] ==
ALAssetTypePhoto)
{
if ([dict objectForKey:UIImagePickerControllerOriginalImage])
{
UIImage* image=[dict
objectForKey:UIImagePickerControllerOriginalImage];
_arrimage.image;
}
}
}
}
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
You can use CreolePhotoSelection to get and select multiple images. It will help to you gets multiple selection and retrieve all selected value into delegate method.
Below is the sample code for it:
CreolePhotoSelection *objCreolePhotoSelection= [[CreolePhotoSelection alloc] initWithNibName:#"CreolePhotoSelection" bundle:nil];
objCreolePhotoSelection.strTitle = YOUR_TITLE_FOR_PHOTO_VIEW;
objCreolePhotoSelection.delegate = self; // It will help to retrive all selected photos from CreolePhotoSelection
objCreolePhotoSelection.arySelectedPhoto = YOUR_CURRENT_ARRAY; // You need to pass same array which is holding selected image
objCreolePhotoSelection.maxCount = YOUR_MAX_PHOTOS_LIMIT; // Eg. 5, 10 anythings...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:objCreolePhotoSelection];
[self.navigationController presentViewController:navController animated:YES completion:nil];
// Delegate Method
#pragma mark - Photo selected Delegate Method
-(void)getSelectedPhoto:(NSMutableArray *)aryPhoto
{
// You will selected image array into aryPhoto object and you can use it
}
First of all an Image View is to show a single image. From the piece of code you shared I am not clear what did you mean by
UIImage* image=[dict
objectForKey:UIImagePickerControllerOriginalImage];
_arrimage.image;
What does this _arrimage mean? Is that a reference to the Image view? then it should be
_arrimage.image = image; //the image fetched in the above line
But this won't fix your issue as this will show only the very last object found in the for loop (probably the last selected image).
To show all selected images it is better to go with a collection view with the array of images as the data source and load each images in each cells of the collection view.
If you want to show all images within the same image view, there is another option by setting it as animationimages
imageView.animationImages = imagesListArray //the list of image you selected
imageView.animationDuration = 2.0
imageView.startAnimating()
This just show each images one at a time from the array with a time interval of 2 seconds
You can't Show Multiple Images in a single ImageView at a same time but yes you can show multiple images on a same ImageView for sometime using Image Animation.
var imagesArray = [UIImage]()
for imageName in 1...3
{
imagesArray.append(UIImage(named: "\(imageName).png")!)
}
// You can also use below code to add images if not want to use loop
// imagesArray.append(UIImage(named: "a.png")!)
// imagesArray.append(UIImage(named: "b.png")!)
// imagesArray.append(UIImage(named: "c.png")!)
self.imageView.animationImages = imagesArray
self.imageView.animationDuration = 2.0
self.imageView.startAnimating()

Passing a UIImage from a viewcontroller to another

I want to pass a UIImage from a View Controller to another one, but it doesn't work. I actually get (null) if I log the UIImage Value in the second View Controller.
The Image File comes from parse.com. And it works absolutely fine in the first view controller. But as soon as I pass it to the second view controller, the image won't work there.
Here's the code:
My prepareForSegue in the .m file of the firstViewController (BookListTableViewController)
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"ShowBookDetailSegue"]) {
NSIndexPath *indexPath = [self.bookListTableView indexPathForSelectedRow];
ParseBookDetailTableViewController *pbdtvc = segue.destinationViewController;
PFObject *tempObject = [totalStrings objectAtIndex:indexPath.row];
PFFile *eventImage = [tempObject objectForKey:#"bookImage"];
if(eventImage != NULL)
{
[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
{
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
pbdtvc.bookImageDetail = thumbnailImage;
}];
}
my .h file of the secondViewController (ParseBookDetailViewController)
#property (strong, nonatomic) UIImage* bookImageDetail;
#property (strong, nonatomic) IBOutlet UIImageView *bookImageDetailView;
viewDidLoad of my .m file of the secondViewController (ParseBookDetailViewController)
NSLog(#"%#",bookImageDetail);
[bookImageDetailView setImage:bookImageDetail];
As I said: in the original View Controller the Image is correct. I can log it and even set it there to an ImageView. But in the second view controller there is (null).
Glad for help, thanks..
EDIT:
This is the code from my cellForRowAtIndexPath.
PFObject *tempObject = [totalStrings objectAtIndex:indexPath.row];
//cell.textLabel.text = [tempObject objectForKey:#"bookTitle"];
cell.titleTextField.text = [tempObject objectForKey:#"bookTitle"];
cell.bookAutor1Label.text = [tempObject objectForKey:#"bookAutor1"];
cell.isbnLabel.text = [tempObject objectForKey:#"bookISBN"];
cell.statusLabel.text = [tempObject objectForKey:#"bookStatus"];
cell.yearLabel.text = [tempObject objectForKey:#"BookDate"];
if ([cell.statusLabel.text isEqualToString:#"nicht verfügbar"]) {
cell.statusLabel.tintColor = [UIColor redColor];
cell.dotImageIcon.image = [UIImage imageNamed:#"dot_red.png"];
}else if ([cell.statusLabel.text isEqualToString:#"verfügbar"]){
cell.statusLabel.tintColor = [UIColor greenColor];
cell.dotImageIcon.image = [UIImage imageNamed:#"dot_green.png"];
}
PFFile *eventImage = [tempObject objectForKey:#"bookImage"];
if(eventImage != NULL)
{
[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
{
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
prepareForSegueImage = thumbnailImage;
[cell.bookImageView setImage:thumbnailImage];
}];
}
At the bottom I have set the Image (that works) in a new property (prepareForSegueImage). NOW its loaded.
And now I've tried to pass only the prepareForSegueImage to the new view controller, but it actually doesn't pass the one from the cell, it passes the last one loaded in the whole table view from the first view controller.
I'm guessing:
[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
{
UIImage *thumbnailImage = [UIImage imageWithData:imageData];
pbdtvc.bookImageDetail = thumbnailImage;
}];
is an asynchronous request happening in the background, that will take some time to return. But your code is expecting it to happen instantly.
Load the image first and call the segue in the completion block,
EDIT
If it works in the first viewController (meaning you have the image) why are you requesting it again? why not simply pass the image you already have?
EDIT 2:
As explained in my comment, your variable is being overwritten each time the cellForRowAtindexPath is called. You could either wrap that code in an if statement, checking for an index or a certain image etc. or you can use the below code to get a specific cell and pull the image form that.
[tblView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:2 inSection:0]];
If you have the image downloaded and have a relatively easy means of getting a reference to it you should not be downloading it again. This could cost the user on a 3G data plan as well as waste resources.
This happens because you are using the getDataInBackgroundWithBlock, which is an asynchronous call. As this happens in a background thread, it is probably not finished when you segue to the other viewcontroller.
You said it works fine in the first controller. I assume this means you have already downloaded the image. Put that in a property instead, and send this property to the next controller.

MWPhotoBrowser does not show display image name for single image in navigation title

I am using MWPhotoBrowser to display images in navigation. For multiple images it is working fine. But when displaying one image(number of images is 1) it does not display image name as title of navigation bar. How do i fix that? here is the code of delegate method:
- (void) photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index {
NSLog(#"Photo displayed");
if (index < [self.imagesGallery count]) {
NSString *imageName = [self.imagesGallery objectAtIndex:index];
photoBrowser._titleText = [imageName stringByDeletingPathExtension];
NSLog(#"Title: %#", photoBrowser._titleText);
NSString *element = [[[imageName stringByDeletingPathExtension] componentsSeparatedByString:#"-"] objectAtIndex:1];
int index_ = [deficiencesShort indexOfObject:element];
photoBrowser._actionButton.title = [NSString stringWithFormat:#"Go To %# Deficiency", [deficiences objectAtIndex:index_]];
}
}
For single image, NSLog(#"Title: %#", photoBrowser._titleText) displays image name as output but does not set as navigation bar title
Maybe you make some changes with MWPhotoBrowser library, show me your code.
Setting title of of MWPhotoBrowser's Navigation:
add name property in MWPhoto
modifier updateNavigation method, in handleMWPhotoLoadingDidEndNotification, pass a title in updateNAvigation (two functions in MWPhotoBrowser.m)
Put below condition in updateNavigation method in MWPhotoBrowser.m file
remove self.title = nil;and add this condition in else part.
if ([_delegate respondsToSelector:#selector(photoBrowser:titleForPhotoAtIndex:)]) {
self.title = [_delegate photoBrowser:self titleForPhotoAtIndex:_currentPageIndex];
}
else {
self.title = nil;
}
Make sure you need to implement photoBrowser:titleForPhotoAtIndex delegate.

Parse data image shows in iOS simulator but not in device

I'm using a collection view controller to show thumbnails. Click on the thumbnail and a segue opens to the full image modally. It works fine on the simulator, but not on my iphone or ipad. The full image is blank. The "comment" shows up in all devices.
Here's the segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showBandPhoto"]) {
NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
BDBPhotoViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
PFObject *tempObject = [imageObjectsArray objectAtIndex:indexPath.row];
PFFile *imageFile = [tempObject objectForKey:#"image"];
NSData *imageData = [imageFile getData];
UIImage *image = [UIImage imageWithData:imageData];
destViewController.bandImageName = image;
NSLog(#"image is %#", image);
NSString *commentGet = [tempObject objectForKey:#"comment"];
destViewController.comment = commentGet;
Here's the code for the photo controller viewDidLoad:
self.photoImageView.image = bandImageName;
self.commentLabel.text = comment;
Instead of using UIImageView, try using a PFImageView. This subclass makes it much easier for loading image data directly from Parse.
I'm not sure why this worked, but I moved the retrieval from Parse to viewDidLoad and created the image array there. When the segue is called it gets the image from the array, rather than having to query Parse.
For me the problem was that I added extension in image naming. The simulator could read the image but not the device. when I removed extension it worked.
Furthermore, as explained in this post:
images in iphone app appear in simulator but not when compiled to device
Mac file system is case-insensitive and iOS file system is case sensitive. Your problem is maybe juste due to image naming.

Parse.com image gallery open image detail view

I haven't been programming for very long and I am trying to create a simple image gallery using Parse.com, I have followed this tutorial https://www.parse.com/tutorials/saving-images but using storyboards instead of nibs.
I managed to got most of it working but I'm stuck at the last hurdle, when it comes to opening the selected image full size in a new view.
Following other answers given around the web I have tried to pass the image to the detail view in - (void)prepareForSegue: but I'm still having no luck.
My code in ViewController.h currently looks like this
- (void)buttonTouched:(id)sender {
PFObject *theObject = (PFObject *)[allImages objectAtIndex:[sender tag]];
PFFile *theImage = [theObject objectForKey:#"imageFile"];
NSData *imageData;
imageData = [theImage getData];
selectedPhoto = [UIImage imageWithData:imageData];
[self performSegueWithIdentifier:#"goGo" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:#"goGo"]) {
PhotoDetailViewController *pdvc = [[PhotoDetailViewController alloc] init];
pdvc.selectedImage = selectedPhoto;
}
}
and in DetailViewController.h
- (void)setDetailImage {
self.photoImageView.image = selectedImage;
}
When it comes to loading the image the view opens blank, any help on this would be a massive help. If it makes it easier I can upload the project.
Thanks in advance,
Chris
Your actual prepareForSegue statements does nothing : you're initializing selectedImage to a totally new PhotoDetailViewController but not the one which will be presenting.
Try to replace
PhotoDetailViewController *pdvc = [[PhotoDetailViewController alloc] init];
by
PhotoDetailViewController *pdvc = (PhotoDetailViewController *)segue.destinationController;

Resources