这个问题开始在 iOS7 中使用新的 UIPickerView 控制器发生。要在 UIPickerView 控制器中使用图像,您必须使用委托方法返回图像:
pickerView:viewForRow:forComponent:reusingView:
问题是屏幕随后会出现各种奇怪的行为 - 当您在控件上下移动手指时,图像视图会消失。
这个问题开始在 iOS7 中使用新的 UIPickerView 控制器发生。要在 UIPickerView 控制器中使用图像,您必须使用委托方法返回图像:
pickerView:viewForRow:forComponent:reusingView:
问题是屏幕随后会出现各种奇怪的行为 - 当您在控件上下移动手指时,图像视图会消失。
这是从 iOS 7.0.2 开始在开发论坛上发布的解决方案:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
// self.myImages is an array of UIImageView objects
UIView * myView = [self.myImages objectAtIndex:row];
// first convert to a UIImage
UIGraphicsBeginImageContextWithOptions(myView.bounds.size, NO, 0);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// then convert back to a UIImageView and return it
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
return imageView;
}
There is a far simpler way to do it than Ed Trujilo's method (It assumes you are using UIImageView's however ... Ed's method should work for any UIView, I believe).
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
return [[UIImageView alloc] initWithImage: [mpSelections[row] image]];
}