iOS7 UIPickerView无法正确显示包含图像的自定义视图

Ed *_*llo 5 uipickerview uikit ios7

iOS7中使用新的UIPickerView控制器开始出现此问题.要在UIPickerView控制器中使用图像,必须使用委托方法返回图像:

pickerView:viewForRow:forComponent:reusingView:

问题是屏幕随后出现各种奇怪的行为 - 当您在控件上上下移动手指时,图像视图会消失.

Ed *_*llo 13

这是一个发布在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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这非常有效.谢谢! (2认同)
  • 知道这里发生了什么吗?也许是开发论坛的链接? (2认同)