UIImageView保持空白

时间:2015-07-16 15:57:23

标签: ios uiimageview uiimage

我创建了一个ViewController,然后将ImageViewController附加到它。 这是我的界面和实现。当我运行程序时,它不显示图像,只显示空白视图。

#import <UIKit/UIKit.h>

@interface ImageViewController : UIViewController
@property (nonatomic, strong) UIImageView *myImageView;

@end

实施

#import "ImageViewController.h"

@interface ImageViewController ()

@end


@implementation ImageViewController

@synthesize myImageView;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //create an image

    //create an image
    UIImage *myScreenShot = [UIImage imageNamed:@"lips_PNG6197.png"];

    //image view instance to display the image
    self.myImageView = [[UIImageView alloc] initWithImage:myScreenShot];

    //set the frame for the image view
    CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                                self.myImageView.frame.size.height/2);
    [self.myImageView setFrame:myFrame];

    //If your image is bigger than the frame then you can scale it
    [self.myImageView setContentMode:UIViewContentModeScaleAspectFit];

    //add the image view to the current view
    [self.view addSubview:self.myImageView];
   }

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

请注意,我在调试器中检查过UIImage不是nil。我也不是通过IB创建UIImageView。 关于出了什么问题的任何建议,即为什么我没有看到图像。

3 个答案:

答案 0 :(得分:2)

如果您没有在IB中创建图像视图,那么这就是您的问题:

CREATE UNIQUE NONCLUSTERED INDEX [NonClusteredIndex-20150203-143420] ON [Sample].[vts_tbVoterAnswers]
(
    [AnswerID] ASC,
    [VoterID] ASC,
    [SectionNumber] ASC,
    [SurveyID] ASC,
    [CaseID] ASC,
    [versionID] ASC,
    [recordstatus] ASC,
    [dateupdated] ASC,
    [voterAnswerStatus] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


ALTER TABLE [Sample].[vts_tbVoterAnswers] ADD  CONSTRAINT [PK_vts_tbVoterAnswers] PRIMARY KEY CLUSTERED 
(
    [VoterAnswerID_PK] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

USE [AdjudicateV3]
GO

/****** Object:  Index [TBVOTER_DECISION_IDX]    Script Date: 7/16/2015 12:27:20 PM ******/
CREATE NONCLUSTERED INDEX [TBVOTER_DECISION_IDX] ON [Sample].[vts_tbVoterAnswers]
(
    [recordstatus] ASC
)
INCLUDE (   [VoterID],
    [AnswerID],
    [SectionNumber],
    [SurveyID],
    [CaseID],
    [versionID],
    [dateupdated]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


USE [AdjudicateV3]
GO

/****** Object:  Index [TBVOTERANSWERS_CASEID_INX]    Script Date: 7/16/2015 12:27:38 PM ******/
CREATE NONCLUSTERED INDEX [TBVOTERANSWERS_CASEID_INX] ON [Sample].[vts_tbVoterAnswers]
(
    [AnswerID] ASC,
    [recordstatus] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


USE [AdjudicateV3]
GO

/****** Object:  Index [TBVOTERSANSWERS_PK]    Script Date: 7/16/2015 12:27:51 PM ******/
CREATE NONCLUSTERED INDEX [TBVOTERSANSWERS_PK] ON [Sample].[vts_tbVoterAnswers]
(
    [CaseID] ASC,
    [recordstatus] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


USE [AdjudicateV3]
GO

/****** Object:  Index [vts_tbVoterAnswers_PK]    Script Date: 7/16/2015 12:28:10 PM ******/
CREATE UNIQUE NONCLUSTERED INDEX [vts_tbVoterAnswers_PK] ON [Sample].[vts_tbVoterAnswers]
(
    [SurveyID] ASC,
    [CaseID] ASC,
    [versionID] ASC,
    [VoterID] ASC,
    [AnswerID] ASC,
    [dateupdated] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

你告诉它的宽度和高度应该基于 CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width, self.myImageView.frame.size.height/2); 而尚未创建,所以它是0.所以这就是你的本质所说:

self.myImageView

我假设你可能想要的是这个:

CGRectMake(10.0f, 10.0f, 0.0, 0.0);

答案 1 :(得分:1)

问题在这里

//set the frame for the image view
CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                            self.myImageView.frame.size.height/2);
[self.myImageView setFrame:myFrame];

调试这些行,您会注意到self.myImageView.frame.size.heightself.myImageView.frame.size.width实际上为零。

self.myImageView = [[UIImageView alloc] initWithImage:myScreenShot];不会为您设置帧大小。

如果您要使用图像的高度/宽度,请使用myScreenShot.size,如果您要使用视图的框架,则self.view.frame.size

答案 2 :(得分:1)

已经给出的答案都指向正确的方向,如果你想让imageview成为图像的大小,你可以替换这行代码

CGRect myFrame = CGRectMake(10.0f, 10.0f, self.myImageView.frame.size.width,
                                self.myImageView.frame.size.height/2);

有了这个

CGRect myFrame = CGRectMake(10.0f, 10.0f, myScreenShot.size.width,
                                myScreenShot.size.height);

我不知道你为什么要这样做。使用您熟悉的框架初始化UIImageView更安全,因此可能尝试使用不同的值来适应您的设计。 e.g

CGRect myFrame = CGRectMake(10.0f, 10.0f, 300.0f,250.0f);