5

我在 iphone 上工作,我采用 UIView 的子类,并在 draw rect 方法中进行一些设计。我想将此视图转换为 .png 格式。

提前致谢。

4

2 回答 2

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];
于 2012-05-25T05:15:37.387 回答
0
   UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(YourView.frame.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(keyWindow.bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [keyWindow.layer renderInContext:context];   
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
    UIGraphicsEndImageContext();
于 2012-05-25T07:53:32.313 回答