Мой UIView или UIImage всегда пустым - PullRequest
0 голосов
/ 21 декабря 2011

Я пытаюсь сделать снимок с моего UIView, а именно я делаю скриншот с моего UIView, но всегда UIImage пусто, и я не знаю почему. И я проверил с моим iPhone и сфотографировать в пустой. (

Я поставил #import <QuartzCore/QuartzCore.h> и добавил QuartzCore.framework.

Может ли кто-нибудь мне помочь?

Вот мой код:

- (IBAction)savePhoto:(id)sender
{    
    UIImage *imageCaptured = [self imageFromView:self.view];

    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];

    // Write a UIImage to JPEG with minimum compression (best quality)
    [UIImagePNGRepresentation(imageCaptured) writeToFile:pngPath atomically:YES];

    // Let's check to see if files were successfully written...

    // Create file manager
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];

    // Point to Document directory
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    // Write out the contents of home directory to console
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
}

- (UIImage *) imageFromView:(UIView *)view 
{    
    UIGraphicsBeginImageContext(CGSizeMake(320,372));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer drawInContext:context];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [imageView setImage:screenShot];
    return  screenShot;
}

1 Ответ

2 голосов
/ 02 марта 2012

Попробуйте изменить строку с

 [view.layer drawInContext:context];

на

[view.layer renderInContext:context];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...