NSImageview 一次更改两个活动视图控制器

NSImageview change in two active viewcontrollers at a time

我有两个 viewcontrollers 1) Profile.h &.m, 2) settings.h &.m. 两者都有 NSimageview,所以当我在设置上更改 imageview 时,然后如何在打开的 viewcontroller 配置文件上同时更改 imageview。

使用通知或委托方法。

通知方式:

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];
 }

我做了一个演示。在该演示中,我使用 UIImagePicker 选择照片,然后在 UIImage 中显示它,并在其他页面中传递相同的图像。所以你可以用同样的方式做这件事。只需将图像存储在任何变量中并将该变量传递给其他页面即可。我的意思是当您在设置 ViewController 中更改图像时,将该图像存储在任何变量中并在您的配置文件 ViewController 中获取该变量并在您的配置文件 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