将uiview转换为.png图像

Cha*_*dni 5 iphone xcode objective-c

我正在研究iphone,我采用了UIView的子类,并且在绘制rect方法中我正在进行一些设计.我想以.png格式转换此视图.

提前致谢.

Dha*_*wal 22

    UIGraphicsBeginImageContext(myView.frame.size);
    [myView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data=UIImagePNGRepresentation(viewImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *strPath = [documentsDirectory stringByAppendingPathComponent:@"myimage.png"];
    [data writeToFile:strPath atomically:YES];
Run Code Online (Sandbox Code Playgroud)