ios - 未通过第一个 View Controller 接收 UIImage

标签 ios objective-c uiimage

您好,我目前只是“认为我已经通过 prepare for segue 破解了发送内容”,但问题是我的图像没有显示在第二个 View Controller 中。因为我是新手,所以我想知道我是否遗漏了什么,因为无论如何我似乎都无法让我的第一个 View Controller 的图像显示在我的第二个 View Controller 中。

GroupsViewController.h

#import <UIKit/UIKit.h>
@interface GroupsViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate> 
@property (weak, nonatomic) IBOutlet UICollectionView *GroupsCollectionView;

- (IBAction)cellToggleAction:(id)sender;

@end

GroupsViewController.m

#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"


@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
}

@end

@implementation GroupsViewController
{
    NSString *reuseIdentifier;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self GroupsCollectionView]setDataSource:self];
    [[self GroupsCollectionView]setDelegate:self];
    reuseIdentifier= @"SmallIcon"; //set inital value of reuse identifier

    arrayOfImages = [[NSArray alloc]initWithObjects:@"a.png", nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A", nil];
}

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

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayOfDescriptions count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:indexPath];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"])
    {
        NSIndexPath* indexPath = [[self.GroupsCollectionView indexPathsForSelectedItems]firstObject];
        if(indexPath !=nil)
        {
            //NSString *selectedDescription = arrayOfDescriptions[indexPath.item]; //collects description from cell
            NSString *selectedImage = arrayOfImages [indexPath.item]; //collects image from cell

            GroupsHomeViewController *groupsHomeViewController = segue.destinationViewController;
            groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImage];
            //groupsHomeViewController.groupLabel = [:selectedDescription];

        }
    }
}


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

@end

GroupsHomeViewController.h

#import <UIKit/UIKit.h>

@interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UIImage *logoImage;
@property (strong, nonatomic) IBOutlet UILabel *groupLabel;

@end

GroupsHomeViewController.m

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

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

@end 

提前感谢您的帮助。

最佳答案

李苏格登,

传递图像是不够的:) 如果你想让它显示在你的屏幕上,你应该将它加载到 ImageView 中,不是吗:)

现在要么以编程方式创建 UIImageView,要么在 Storyboard中添加一个 imageView 绘制一个 IBOutlet 并将图像设置为

self.yourImageView.image = self.logoImage;

否则以编程方式创建一个 UIImageView,

UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.logoImage.size.width,self.logoImage.size.height)];
  yourImageView.image = self.logoImage;
  [self.view addSubview:yourImageView];

关于ios - 未通过第一个 View Controller 接收 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470384/

相关文章:

iphone - iPhone 的 Thrift 框架

objective-c - UIImage setter NSInvalidArgumentException

ios - swift 4 : UIImage from URL

iphone - 在 iphone 上一次播放一个不同声音的最佳方式?

ios - 应用已上线但未提交 InApp Purchase

iphone - 将图像文件上传到 Web 表单

ios - map 上未显示 MapBox 多段线

ios - 如何解决这个 "index 0 beyond bounds for empty array! "

ios - 动态缩放 UIButton 中的字体

ios - 如何将动画 GIF 拆分为一组 UIImages?