iOS how index of image can be passed from collectionview to scrollview - ios

I am creating a program where if I click on thumbnail in collectionview, larger image should be opened in scroll view which is in another view collector. For that purpose i am using segue. but I am doing something wrong there, how can I solve this problem,
my code of segue is,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"])
{
NSIndexPath *selectedIndexPath = [[self.gallerycollection indexPathsForSelectedItems] objectAtIndex:0];
// load the image, to prevent it from being cached we use 'initWithContentsOfFile'
NSString *imageNameToLoad = [NSString stringWithFormat:#"interior_%d", selectedIndexPath.row];
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:#"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];
GalleryImageScrollViewController *gallerydetailViewController = [segue destinationViewController];
gallerydetailViewController.FullScreenImageScroller=image ;
}
}
detailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *mutablearray = [NSMutableArray array];
data=[MyDatabase new];
slideImages=[data OpenMyDatabase:#"SELECT pic_name_big FROM interior":#"pic_name_big"];
[mutablearray addObjectsFromArray:slideImages];
temparr = [[NSMutableArray alloc]init];
temparr=[NSMutableArray arrayWithArray:mutablearray];
[self putImageViewsInScrollView:[temparr count]];
self.FullScreenImageScroller.delegate=self;
}
-(void) putImageViewsInScrollView:(int)numberOfImageViews
{
for(int i=0 ;i<numberOfImageViews; i++)
{
fullScreenImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[temparr objectAtIndex:i]]];
fullScreenImageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) , 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[self.FullScreenImageScroller addSubview:fullScreenImageView];
}
[self.FullScreenImageScroller setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([temparr count]), HEIGHT_OF_IMAGE)];
[self.FullScreenImageScroller setContentOffset:CGPointMake(0, 0)];
[self.FullScreenImageScroller scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
then what should I pass in scrollview controller (GalleryImageScrollViewController) to open image of specific thumbnail ?

Pass the image name to the detail view controller like this
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems];
DetailViewController *dest = [segue destinationViewController];
dest.imageName = [recipeImages objectAtIndex:[[indexpaths objectAtIndex:0] row]];
// imageName is a property of detail view controller
}
Then in the viewDidLoad of detailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:self.imageName];
}
Im not using scroll view, only an imageView added as a sub view to the view

Its pretty simple. Just pass the index of selected thumbnail image to GalleryImageScrollViewController.(from answers given above..)
Then, In the viewDidLoad method of detailViewController populate image in imageView using the fetched index.
In the viewDidLoad of detailViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = [UIImage imageNamed:[[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"interior_%d", indexOfThatImage] ofType:#"png"]]];
}
Please note that file extension is correct in that of your bundle.

Related

UIWebView from UITableview from TabBarController not loading

Trying to load a url being passed from the previous viewcontroller in a tabviewcontroller sequence. Checked the usual suspects. UIWebview is added view outlet. All the proper delegate are connected. I'm not sure if the NSURL is being passed to the next view controller as I get a black screen (although the response object isn't nil). It's driving me crazy because it's a simple problem and it's something I've done millions of times but it's not working. Any help would be appreciated.
Below you'll find the code.
SquirrelInformationTableViewController.m
#interface ASAInformationTabViewController (){
NSMutableArray *squirrelArray;
}
#property (nonatomic, strong) ODRefreshControl *refreshControl;
#property (weak, nonatomic) IBOutlet UITableView *squirrelListTableView;
#end
#implementation ASAInformationTabViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpSquirrelList];
//Add drag to refresh function
self.refreshControl = [[ODRefreshControl alloc]initInScrollView:self.squirrelListTableView];
self.refreshControl.tintColor = [UIColor colorWithRed:46.0/255 green:172.0/255 blue:247.0/255 alpha:1];
[self.refreshControl addTarget:self action:#selector(dragToRefreshAction:) forControlEvents:UIControlEventValueChanged];
}
-(void)setUpSquirrelList
{
__weak UITableView* weaktable = self.squirrelListTableView;
[[ASASquirrelManager instance] listSquirrels:^(NSString *response, NSMutableArray *result) {
if ([response isEqualToString:CODE_SUCCESS]) {
squirrelArray = result;
dispatch_async(dispatch_get_main_queue(), ^{
if (weaktable) {
[weaktable reloadData];
}
});
}
}];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ASASquirrelInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"squirrelIdentifier"];
ASASquirrelModel *squirrelLocal = squirrelArray[indexPath.row];
cell.squirrelTitleLabel.text = squirrelLocal.title;
cell.squirrelDescLabel.text = squirrelLocal.desc;
NSData *imageData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:squirrelLocal.image]];
cell.squirrelImage.image = [UIImage imageWithData:imageData];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ASASquirrelModel *squirrelWebLocal = squirrelArray[indexPath.row];
NSString *squirrelURLString = squirrelWebLocal.url;
//create an array with the string you want an access it
NSLog(#"%#", urlStringArray);
//Create a string from the URL pass it to the sdvc.string
SquirrelDetailViewController *sdvc = [SquirrelDetailViewController new];
sdvc.urlString = squirrelURLString;
[self.navigationController pushViewController:sdvc animated:YES];
}
SquirrelDetailViewController.m
#interface SquirrelDetailViewController () <UIWebViewDelegate>
#end
#implementation SquirrelDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webView.frame = self.view.bounds;
[self.view addSubview:self.webView];
}
-(void)viewWillAppear:(BOOL)animated
{
self.urlstr = [NSURL URLWithString:self.urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.urlstr];
[self.webView loadRequest:requestObj];
}
I think you need to create the instance of the sdvc using storyboard otherwise it will not be getting instantiated properly. So:
SquirrelDetailViewController *sdvc =
(SquirrelDetailViewController *)[[UIStoryboard storyboardWithName:#"XXX" bundle:nil] instantiateViewControllerWithIdentifier:#"YYY"];
where XXX is your storyboard name and YYY is the storyboard identifier for SquirrelDetailViewController.
I found out that the trouble was I wasn't preparing for segue. (hits forehead*)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"webviewpush" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"webviewpush"]) {
// Get destination view
SquirrelDetailViewController *vc = [segue destinationViewController];
NSIndexPath *path = [self.squirrelListTableView indexPathForSelectedRow];
ASASquirrelModel *squirrelWebLocal = squirrelArray[path.row];
NSString *squirrelURLString = squirrelWebLocal.url;
vc.urlString = squirrelURLString;
}
}
If you'll excuse me I'm going to go drown myself now.

Send captured image to another UIViewController with prepareForSegue

I'm trying to send the captured image to another UIViewController.
So when I press captured button, it's taking a photo and I can save the image in my camera roll. But when I want to see image in another view I can't see it.
This is my code :
- (IBAction)captureButton:(id)sender
{
[self.cameraViewController captureImageWithCompletionHander:^(id data) {
self.image = ([data isKindOfClass:[NSData class]]) ? [UIImage imageWithData:data] : data;
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
[self performSegueWithIdentifier:#"savingSegue" sender:self];
}];
}
And this is the prepare for segue method.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = [[PhotoSaveViewController alloc] init];
[pvc.imageView setImage:self.image];
}
}
If your second view is a PhotoSaveViewController then replace your prepareForSeque method with this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = (PhotoSaveViewController *)segue.destinationViewController;
if (pvc)
[pvc.imageView setImage:self.image];
}
}
Indeed, it's not your job to instantiate the destination UIViewController, your UINavigationController have already done that.
I guess that imageView on PhotoSaveViewController it is outlet and it is not set at this moment. You should declare there some other property like
#property (nonatomic, retain) UIImage *inputImage;
and assign to that in prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"savingSegue"]) {
PhotoSaveViewController *pvc = segue.destinationViewController;
pvc.inputImage = self.image;
}
}
Then do the following in the PhotoSaveViewController:
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = self.inputImage;
}
Another approach you can create it from code totally. Assign Storyboard ID to the PhotoSaveViewController and then do the following:
- (IBAction)captureButton:(id)sender {
[self.cameraViewController captureImageWithCompletionHander:^(id data) {
self.image = ([data isKindOfClass:[NSData class]]) ? [UIImage imageWithData:data] : data;
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
PhotoSaveViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:#"PhotoSaveViewControllerID"];
dvc.inputImage = self.image;
[self.navigationController pushViewController:dvc animated:YES];
}];
}

Passing UIImage with segue

I am reading data from the JSON object and pass the image property to other viewcontroller. Before I do it, I wanted to test the following code with hardcoded UiImage and it works perfectly fine.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
GrillMenuSingleFoodViewController *gVC = [segue destinationViewController];
if ([segue.identifier isEqualToString:#"isMoreDetail"]) {
[gVC setImage:[UIImage imageNamed:#"Ali_nazik.jpg"]];
}
}
But the following code is not working. Even though I am getting imagedata, once I pass to other viewcontroller it shows nil.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
GrillMenuSingleFoodViewController *gVC = [segue destinationViewController];
if ([segue.identifier isEqualToString:#"isMoreDetail"]) {
NSString *imageURLString=[menuArray[indexPath.row]valueForKey:#"picture"];;
NSURL *url = [NSURL URLWithString:imageURLString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
[gVC setImage:[UIImage imageWithData:data]];
}
}
Change your "#property" declaration to strong.
I.E.
#property (strong) UIImage *image;
You need to have a retained image for it to be picked up in your new view controller.
Otherwise it will be disposed by the parent view controller when the "prepareForSegue" method finishes.

pfquery pass pfobject to another VC

I want to pass PFObject from VC1 to VC2. I know thats easy if I pass PFObject from Tableview to VC. Anyway, I have A VC with Tableview with the segue to detailVC (1) with ImageView. This ImageView would be shown in detailiamgeviewcontroller after tap image.(2) And I want to send pfobject to this Controller. But have no idea how to.
Any idea?
(1)
if ([segue.identifier isEqualToString:#"showDetaill"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PFObject *object = [self.objects objectAtIndex:indexPath.row];
DetailViewController2 *detailViewController = [segue destinationViewController];
detailViewController.objecteditem = object;
}
(2)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.imageviewsecond setImageWithURL:[NSURL URLWithString:[self.objecteditem objectForKey:#"VlajkaURL"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[imageviewsecond addGestureRecognizer:singleTap];
}
- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
[self performSegueWithIdentifier:#"sendi" sender:self];
NSLog(#"image tapped!!!");
}
Do the same as you are already doing. In view 1 you are sending the object to view 2 (DetailViewController2). In DetailViewController2, in the prepareForSegue method, you do the same:
if ([segue.identifier isEqualToString:#"sendi"])
{
DetailImageViewController *detailImageViewController = [segue destinationViewController];
detailImageViewController.somePropertyName = self.objecteditem;
}

Passing image between views

I am solving this issue about 2 weeks and only be able to pass string, not image.
I store the image from camera or from library in this method.
-(IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc]init];
trick.trickName = self.trickLabel.text;
trick.trickPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(0, 356, 320, 305)];
trick.trickPhoto.image = self.ImagePhoto.image;
[[Tricks trickList]addObject:trick];
}
In tableViewClass I store the values into properties of detailView
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"detailTrick"]){
NSIndexPath *indexPath = nil;
indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
Tricks *trick = [[Tricks trickList] objectAtIndex:indexPath.row];
detailViewController.trickPhoto = [[UIImageView alloc]initWithFrame:CGRectMake(0, 358, 200, 200)];
detailViewController.fileText = trick.trickName;
detailViewController.trickPhoto = trick.trickPhoto;
//object = [Tricks trickList][indexPath.row]
}
}
Text showed up every time without problems, but there is no image in detailViewController.Thanks for help.
viewDidLoad of detailViewController
[super viewDidLoad];
[self.detailButton setTitle:[NSString stringWithFormat:#"%#",_fileText] forState:UIControlStateNormal];
[self.trickPhoto setImage:_trickPhoto.image];
First off, trickPhoto within the Trick class should be a UIImage, not a UIImageView. Models shouldn't know anything about views such as frames, etc so right now you're violating the MVC design pattern.
Then it'd be:
- (IBAction)saveAction:(id)sender
{
Tricks *trick = [[Tricks alloc] init];
trick.trickName = self.trickLabel.text;
trick.trickPhoto = self.ImagePhoto.image;
[[Tricks trickList] addObject:trick];
}
The better way of doing this would be to create a Trick property within DetailViewController and just pass the whole trick into the view controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"detailTrick"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
Tricks *trick = [[Tricks trickList] objectAtIndex:indexPath.row];
NSLog(#"Make sure trick isn't nil: %#", trick);
detailViewController.trick = trick;
}
}
Then you'd just populate it with:
[super viewDidLoad];
[self.detailButton setTitle:[NSString stringWithFormat:#"%#", self.trick.trickName] forState:UIControlStateNormal];
[self.trickPhoto setImage:self.trick.trickPhoto];

Resources