0
Я пытаюсь сделать снимок с моего UIView
, а именно сделать снимок экрана с моего UIView
, но всегда UIImage
пуст, и я не знаю почему. И я проверил с моим iPhone и сделал снимок. :(Мой UIView или UIImage всегда в пробеле
Я поставил #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;
}
Не ответ на вашу проблему, но почему вы жестко кодируете размер изображения вместо использования 'view.bounds.size'? – DarkDust
Кроме того, ваш способ обращения к папке «Документы» неверен. Используйте что-то вроде 'NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; ' – DarkDust
Спасибо! У вас есть –