如何将UIView的一部分转换成UIImage并在imageView中显示



我有一个视图,我正在绘制签名,我希望视图的签名部分应转换为UIImage,然后在UIImageView中显示它,这是我从网络获得的代码,我正在使用转换

    UIGraphicsBeginImageContext(signatureView.bounds.size);
   [signatureView.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
   logoImageView.image=img;
   UIGraphicsEndImageContext();

试试这个希望能帮到你。

 - (UIImage *) getUIImageWithmyView:(UImyView *)myView
    {
        UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, 0.0);
        [myView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * myImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return myImage;
    }

使用我的bellow方法…

- (UIImage *)captureView {
     //hide controls if needed
    CGRect rect = [signetureView bounds];//use your signature view's Rect means Frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

你可以像下面这样调用上面的方法…

UIImage *tempImageSave=[self captureView];

try this:

  UIGraphicsBeginImageContext(signatureView.bounds.size);
  [signatureView.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  logoImageView.image=img;

first endimagecontext,然后在imageview中设置/使用该图像

最新更新