[英]UIImage and UIImagePicker and UIImageView
我有一個UISegmentedControl,有一些照片,我的最初目的是,當我單擊某個特定的片段時,一個特定的圖像應出現在視圖中。
例如:如果我有四個段和四個圖像,則在每個段上單擊“我必須看到一個圖像”。
在這里,我使用下面的特定代碼拍攝了一個NSArray並上傳了所有這些圖像:
NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"f.ppg"], [UIImage imageNamed:@"t.ppg"....etc ], nil];
然后,我想將每個特定的圖像附加到細分索引。 這是我編寫的代碼。
-(IBAction) segmentActionChanged:(id)sender
{
int segment = mSegment.selectedSegmentIndex;
mImageView.image=[images objectAtIndex:segment];
}
編譯應用程序時,我沒有顯示任何圖像,並且突然退出。
關於此的任何幫助。
回答問題1和3(我認為)。 要顯示圖像,您將需要在某個位置使用UIImageView
。 一個簡單的方法就是創建這樣的方法。
//somewhere in viewDidLoad perhaps?
//title array is an array of strings to use as the label on each section of your control
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
//this sets the segmentAction: method as the selector for your particular UISegmentedControl
//(so when its value is changed, segmentAction: gets called)
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//the segmentAction: method
- (IBAction) segmentAction:(id)sender
{
//This assumes you have declared the imageView elsewhere, as well as the array and that you
//are using Interface Builder to create/hookup the segmentControl. Basically
myImageView.image = [myImageArray objectAtIndex:sender.selectedSegmentIndex];
}
一些注意事項:這沒有利用延遲加載,因為您是在顯示所有圖像之前將它們創建並保存在數組中。 我建議為UISegmentedControl創建一個實例變量,以便您也可以在任何地方訪問它。 免責聲明:此代碼不完整,並進行了一些初始化。
要回答第二個問題:處理電話/設備的相機或保存的相冊時,將使用UIImagePickerController
。 它包含在文檔中。
好像您缺少保留。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.