1

So I have a view pager that is suppose to allow users to slide through photos the way a user would expect to slide through a album of photos , but the issue comes in when a user slides around and trys to press the "share" button in the navigation bar. It works fine if a user just opens a photo from the album and never trys to slide right or left , but if they do try to slide in either direction and then try to save the photo they have slid to it returns the photo after the photo the user is currently viewing. Ive eliminated this down to something having to do with viewControllerBeforeViewController and viewControllerAfterViewController being called AFTER the current viewcontroller s callehere is my class:

#import "APPChildViewController.h"
#import "CommentsViewController.h"

#import "ViewPhotosPagerVC.h"

@interface ViewPhotosPagerVC ()

@end

@implementation ViewPhotosPagerVC

- (void)viewDidLoad {
    [super viewDidLoad];

   // [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];


    CommentsViewController *initialViewController = [self viewControllerAtIndex:0];
    NSDictionary *dict =[_photoArray objectAtIndex:_startingIndex];
    NSLog(@"DICT:%@",dict);
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    NSLog(@"MSG_ID:%@",msg_id);
    initialViewController.msg_id=msg_id;
    initialViewController.navItem=self.navigationItem;
    initialViewController.delegate=self;

    initialViewController.theIndex=_startingIndex;
    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

- (CommentsViewController *)viewControllerAtIndex:(NSUInteger)index {


    NSLog(@"CommentVC_INDEX:%zd",index);
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommentsViewController *childViewController=[storyboard instantiateViewControllerWithIdentifier:@"pagerPhotoComments"];


    if(_photoArray<=index){

        index=0;
    }

    NSDictionary *dict =[_photoArray objectAtIndex:index];
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    childViewController.msg_id=msg_id;
    childViewController.navItem=self.navigationItem;
    childViewController.delegate=self;
    childViewController.theIndex = index;


    return childViewController;

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSLog(@"BEFORE_PAGEVIEWCONTROL_CALLED");
    NSUInteger index = [(CommentsViewController *)viewController theIndex];

    if (index == 0) {
        NSInteger newIndex=_photoArray.count;
        newIndex--;
        return [self viewControllerAtIndex:newIndex];
    }


    // Decrease the index by 1 to return
    index--;



    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommentsViewController *childViewController=[storyboard instantiateViewControllerWithIdentifier:@"pagerPhotoComments"];


    if(_photoArray<=index){

        index=0;
    }

    NSDictionary *dict =[_photoArray objectAtIndex:index];
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    childViewController.msg_id=msg_id;
    childViewController.delegate=self;
    childViewController.theIndex = index;

    return childViewController;

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSLog(@"AFTER_PAGEVIEWCONTROL_CALLED");

    NSUInteger index = [(CommentsViewController *)viewController theIndex];

    index++;

    if(_photoArray.count<=index){


        index=0;
    }

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    CommentsViewController *childViewController=[storyboard instantiateViewControllerWithIdentifier:@"pagerPhotoComments"];


    if(_photoArray<=index){

        index=0;
    }

    NSDictionary *dict =[_photoArray objectAtIndex:index];
    NSString *msg_id=[dict objectForKey:@"msg_id"];
    childViewController.msg_id=msg_id;
    childViewController.delegate=self;
    childViewController.theIndex = index;


    return childViewController;

}



- (void)viewWillDisappear:(BOOL)animated {


    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

- (void)sharePhoto:(UIImage *)shareImage andText:(NSString *)shareText withDict:(NSDictionary *)dict{
    //NSLog(@"SHARE_IMG_UPDATE_DICT:%@",dict);

    NSString *ALBUM_ID= [dict objectForKey:@"ALBUM_ID"];
    NSString *combinedShareText;
    if(![ALBUM_ID isEqualToString:@"0"]){

        NSArray *album_array=[dict objectForKey:@"album_info"];
        NSDictionary *album_dict=[album_array objectAtIndex:0];

        //NSLog(@"ALBUM_DICT:%@",album_dict);
        NSString *album_title=[album_dict objectForKey:@"title"];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *first_name= [defaults objectForKey:@"first_name"];
        NSString *album_photo_count=[dict objectForKey:@"album_img_count"];


        combinedShareText= [NSString stringWithFormat:@"%@ shared a photo w. you from the event album:%@(%@ photos) ~view the rest of this album on  for IOS",first_name, album_title,album_photo_count];

        NSLog(@"SHARE_TEXT:%@",combinedShareText);

    }else{


        NSString *first_name=[dict objectForKey:@"first_name"];
        NSString *last_name=[dict objectForKey:@"last_name"];
        NSString *message =[dict objectForKey:@"message"];
        NSString *created_string=[dict objectForKey:@"created"];
        double created_double = created_string.doubleValue;
        NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
        NSString *ago = [date timeAgo];
        combinedShareText= [NSString stringWithFormat:@"'%@'~ posted by %@ %@ %@ | You'll thank me later: www.buhz.com",message,first_name,last_name,ago];

        NSLog(@"SHARE_TEXT:%@",combinedShareText);
    }

    if(![ALBUM_ID isEqualToString:@"0"]){

    UIImage *backgroundImage = shareImage;
    UIImage *watermarkImage = [UIImage imageNamed:@"ios_watermark_logo.png"];

    UIGraphicsBeginImageContext(backgroundImage.size);
    [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
    [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - 240, backgroundImage.size.height - 110, 230, 100)];
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();








        UIActivityViewController *controller =
        [[UIActivityViewController alloc]
         initWithActivityItems:@[combinedShareText,result]
         applicationActivities:nil];
         [self presentViewController:controller animated:YES completion:nil];
    }else{

        UIActivityViewController *controller =
        [[UIActivityViewController alloc]
         initWithActivityItems:@[combinedShareText,shareImage]
         applicationActivities:nil];
         [self presentViewController:controller animated:YES completion:nil];

    }









}







-(UIImage*) drawText:(NSString*) text
             inImage:(UIImage*)  image
             atPoint:(CGPoint)   point
{

    NSMutableAttributedString *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",text]];

    // text color
    [textStyle addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, textStyle.length)];

    // text font
    [textStyle addAttribute:NSFontAttributeName  value:[UIFont systemFontOfSize:20.0] range:NSMakeRange(0, textStyle.length)];

    UIGraphicsBeginImageContext(image.size);
    [image drawInRect:CGRectMake(0,0,image.size.width,image.size.height)];
    CGRect rect = CGRectMake(point.x, point.y, image.size.width-(point.x*2), image.size.height);
    [[UIColor whiteColor] set];

    // add text onto the image
    [textStyle drawInRect:CGRectIntegral(rect)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}
- (void)shareUpdate:(NSString *)shareText withDict:(NSDictionary *)dict{

    NSLog(@"SHARE_TEXT_UPDATE_DICT:%@",dict);
    NSString *first_name=[dict objectForKey:@"first_name"];
    NSString *last_name=[dict objectForKey:@"last_name"];
    NSString *message =[dict objectForKey:@"message"];
    NSString *created_string=[dict objectForKey:@"created"];
    double created_double = created_string.doubleValue;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
    NSString *ago = [date timeAgo];
    NSString *combinedShareText= [NSString stringWithFormat:@"'%@'~ posted by %@ %@ %@ | Join me on www..com",message,first_name,last_name,ago];

    NSLog(@"SHARE_TEXT:%@",combinedShareText);

    UIActivityViewController *controller =
    [[UIActivityViewController alloc]
     initWithActivityItems:@[combinedShareText]
     applicationActivities:nil];

    [self presentViewController:controller animated:YES completion:nil];


}

@end
ChuckKelly
  • 1,742
  • 5
  • 25
  • 54

0 Answers0