简体   繁体   English

UIImage和UIImagePicker和UIImageView

[英]UIImage and UIImagePicker and UIImageView

I have a UISegmentedControl and I have some photos, my initial aim is when ever I click on a particular segment one particular image should appear on the view. 我有一个UISegmentedControl,有一些照片,我的最初目的是,当我单击某个特定的片段时,一个特定的图像应出现在视图中。

For example: If i have four segments and four images then upon each segment I click I must see a image. 例如:如果我有四个段和四个图像,则在每个段上单击“我必须看到一个图像”。

Here I have taken an NSArray and Uploaded all these images using this particular below code: 在这里,我使用下面的特定代码拍摄了一个NSArray并上传了所有这些图像:

NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"f.ppg"], [UIImage imageNamed:@"t.ppg"....etc ], nil];

and after this I want to attach each particular image to a segment index. 然后,我想将每个特定的图像附加到细分索引。 So here is the code which I wrote. 这是我编写的代码。

-(IBAction) segmentActionChanged:(id)sender
{
    int segment = mSegment.selectedSegmentIndex; 
    mImageView.image=[images objectAtIndex:segment]; 
}

While compiling the application I am not getting any Images displayed and it is quiting abruptly. 编译应用程序时,我没有显示任何图像,并且突然退出。

Any help regarding this. 关于此的任何帮助。

To Answer questions 1 and 3 (I think). 回答问题1和3(我认为)。 to display an image, you will need a UIImageView somewhere. 要显示图像,您将需要在某个位置使用UIImageView a simple way to do this would be to create an method such as this. 一个简单的方法就是创建这样的方法。

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

A few Notes: this does not take advantage of lazy loading, since you are creating and holding all of your images in an array prior to showing them. 一些注意事项:这没有利用延迟加载,因为您是在显示所有图像之前将它们创建并保存在数组中。 I would suggest creating an instance variable for the UISegmentedControl so you can access it anywhere also. 我建议为UISegmentedControl创建一个实例变量,以便您也可以在任何地方访问它。 Disclaimer: this code is not complete and assumes some initializations. 免责声明:此代码不完整,并进行了一些初始化。

To answer question number two:A UIImagePickerController is used when dealing with the phone's/devices camera or saved photos album. 要回答第二个问题:处理电话/设备的相机或保存的相册时,将使用UIImagePickerController It is covered in the documentation. 它包含在文档中。

好像您缺少保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM