无法在iOS中保存部分屏幕

时间:2015-05-02 23:34:11

标签: ios objective-c

我想截取iphone屏幕的一部分截图,但我不知道为什么我会卡住。我不知道我做错了什么,因为我的执行没有错误。我在这里看到了这个 question,并且一步一步地遵循,但是...我不认为是错误...所以请帮助人们

这是我的代码:

-(void)saveImageBig
{
    UIImage *myBigImage = [self imageFromBig]; // this method returns a UIImage to store inside myBigImage

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //the directory

    NSString *pngFilePath = [NSString stringWithFormat:@"%@/myPic.png",docDir]; // the full path of the image .PNG
    NSLog(@"%@", pngFilePath); //just to check the address
    NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(myBigImage)]; //Create an NSData from the PNG
    [data1 writeToFile:pngFilePath atomically:YES]; //Put the data inside the address as a PNG
}

-(UIImage *)imageFromBig
{
    CGSize size  = CGSizeMake(568.0, 288.0); //a part of the iphone screen
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
    UIImage *bigImage; //The image pointer
    UIImageView *bigImageView;

    [bigImageView.image drawInRect:CGRectMake(16, 20, size.width, size.height)];  //Beginning X16 Y20 take a screenshot of 568X288 (size)
    bigImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(bigImage, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
    return bigImage; //Save the image into albumns
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{ //just the alerts... 
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Image could not be saved.Please try again"  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil];
        [alert show]; //Not saved
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image was successfully saved in photoalbum"  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil];
        [alert show]; //Saved
    }
}

- (IBAction)SaveImageBtn:(id)sender
{
    [self saveImageBig];
}

结果显示图像已成功保存,但图像为空白,空白但该区域有对象和cotrols(大小= 568X288)。

希望你能提前帮助我。

0 个答案:

没有答案