簡體   English   中英

如何在iPhone中自動捕獲圖像?

[英]How to capture the image automatically in iPhone?

我是iPhone開發的新手,我曾使用UIImagePickerController手動捕獲圖像,但是我想自動捕獲圖像,是否有可能做到這一點。 請幫我。

提前致謝

嘗試這個。 在此,我將屏幕快照保存在相冊中

-(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]);
    }
}

您可以使用以下方法獲取保存的圖像

- (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];

}

要獲取屏幕截圖,您可以使用以下代碼。

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

對於Ratina顯示設備,您必須檢查:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM