简体   繁体   中英

How to capture the image automatically in iPhone?

I'm new to iPhone development, I have used UIImagePickerController to capture the image manually, but i want to capture the image automatically, Is it possible to do that. please help me.

Thanks in advance

Try This. In this I have saved the screen shot in photos album

-(void)screenShotCapture
{

    //-- Screen Capture --------------------------------------------------------------------//

    UIImage *image = nil;

    UIGraphicsBeginImageContext(loginBgImgVw.frame.size);

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd-yyyy hh:mm:ss"];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];

    NSString *dateToday = [formatter stringFromDate:[NSDate date]];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(375, 0, 600, 44)];
    [label setText:dateToday];
    NSString *fileNameStr = [NSString stringWithFormat:@"%@_Login_%@",providerIdStr,dateToday];

    [label setText:fileNameStr];
    label.backgroundColor = [UIColor clearColor];
    [loginBgImgVw addSubview:label];
    [formatter release];

    loginBgImgVw.frame = CGRectMake(10, 0, 1006, 669);

    [loginBgImgVw.layer renderInContext: UIGraphicsGetCurrentContext()];     
    image = UIGraphicsGetImageFromCurrentImageContext();        
    UIGraphicsEndImageContext();        
    [label removeFromSuperview];

    //=--

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docspath = [paths objectAtIndex:0];
    NSLog(@"path=--%@",paths);

    NSString *dataPath = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:@"ProviderID_%@",providerIdStr]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];
    }

    NSString *savedImagePath = [dataPath stringByAppendingPathComponent:@"Login_Provider.png"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.2);

    if ([screenCapturNameAry count] != 0)
    {
        if ([screenCapturNameAry containsObject:@"Login"])
        {
            [imageData writeToFile:savedImagePath atomically:NO];

        }

    }
    UIImageWriteToSavedPhotosAlbum([self loadImage:@"Login_Provider"],self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);


    //--------------------------------------------------------------------------------------//

}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
    if (error == nil)
    {
        NSLog(@"Login Image saved successfully.");
    }
    else
    {
        NSLog(@"Error occurred:%@",[error localizedDescription]);
    }
}

You can get the saved image using following method

- (UIImage*)loadImage:(NSString*)imageName 
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"ProviderID_%@",providerIdStr]];

    NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",imageName]];

    return [UIImage imageWithContentsOfFile:fullPath];

}

To take the screenshot you can use the following code.

UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"screen.png" atomically:YES];

For Ratina display device you have to check:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM