NSImageview change in two active viewcontrollers a

2019-08-05 10:26发布

I have two viewcontrollers 1) Profile.h &.m, 2) settings.h &.m. Both have NSimageview, so when i change imageview on settings,thenn at same time how change imageview on opened viewcontroller profile.

2条回答
Root(大扎)
2楼-- · 2019-08-05 11:15

Use notification or Delegate method.

Notification method:

profile.m

-(viewDidLoad) {
   [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(changeImageview) name:@"changeImage" object: nil];
 }


-(changeimageView) {
  //Change Image here
 }

settings.m

-(void)changeImageForSettingsView{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"changeImage" object: nil];
 }
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-08-05 11:21

I have done thing by making one demo. In that demo i am selecting photo using UIImagePicker then displaying it in UIImage and passing that same image in other page. So you can do this thing in same way. Just you store your image in any variable and pass that variable to other page. I mean when you change your image in Setting ViewController, store that image in any variable and fetch that variable in your Profile ViewController and display image in your Profile ViewController.

#import "ImageShowViewController.h"
#import "ViewController.h"
#import "CollectionViewCell.h"

@interface ImageShowViewController ()

{
    NSMutableArray *filelist;
}
//@synthesize collectionview;

@end

@implementation ImageShowViewController
@synthesize data;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,     YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@",[data objectAtIndex:0]]];
    
    // Do any additional setup after loading the view.
  //  NSLog(@"%@", _data);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return data.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";
    
    CollectionViewCell *cell = [self.collectionview dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    
    
    UIImageView *recipeImageView = cell.imgcollection;
    //[recipeImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@""]]]];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,     YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@",[data objectAtIndex:indexPath.row]]];
    recipeImageView.image = [UIImage imageWithContentsOfFile:localFilePath];
    // cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
    
    return cell;
}



@end
#import "ViewController.h"

#import "ImageShowViewController.h"
@interface ViewController ()
{
    NSInteger num;
}
@end

@implementation ViewController

@synthesize savedImagePath,documentsDirectoryPath;

- (void)viewDidLoad {
    [super viewDidLoad];
    num=0;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnMoreOption:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Options"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Select Photos", @"View Photos" ,nil];
   // actionSheet.tag = 100;
    [actionSheet showInView:self.view];
}


//Mehtod for buttons
- (IBAction)btnOnViewPhotos:(id)sender {
    ImageShowViewController  *nxt=(ImageShowViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"view1"];
    nxt.data=_filelist;
    [self.navigationController pushViewController:nxt animated:YES];

}

//Action Sheet Code for selecting options
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];
    
    if ([buttonTitle isEqualToString:@"Select Photos"])
    {
        NSLog(@"Select Photos button");
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:picker animated:YES completion:NULL];
        
    }

    else if ([buttonTitle isEqualToString:@"View Photos"])
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
        _filelist=[[NSMutableArray alloc]init];
        for (NSString *filename in dirContents)
        {
            NSString *fileExt = [filename pathExtension];
            
            if ([fileExt isEqualToString:@"png"])
            {
                
                [_filelist addObject:filename];
            }
        }
        NSLog(@"document folder content list %@ ",_filelist);

        ImageShowViewController  *nxt=(ImageShowViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"view1"];
        nxt.data=_filelist;
        [self.navigationController pushViewController:nxt animated:YES];
    }

    
       
}

//Image Picker Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSDate *time = [NSDate date];
    NSDateFormatter* df = [NSDateFormatter new];
    [df setDateFormat:@"ddMMyyyy-hhmmss"];
    NSString *timeString = [df stringFromDate:time];
   NSString *fileName = [NSString stringWithFormat:@"%@", timeString];
    
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDirectoryPath = [paths objectAtIndex:0];
    
    NSLog(@"View Controller Path:%@",documentsDirectoryPath);
    
    
    savedImagePath = [documentsDirectoryPath
                      stringByAppendingPathComponent:[NSString stringWithFormat: @"%@-%d.png", fileName, num]];
    
    num += 1;
    
    NSData *imageData = UIImagePNGRepresentation(chosenImage);
    [imageData writeToFile:savedImagePath atomically:NO];
    
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
   }
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
}


@end

查看更多
登录 后发表回答