无法在iOS中保存部分屏幕
我想截取iphone屏幕的一部分,但我不知道为什么我被卡住了。我不知道我做错了什么,因为我在执行时没有出错。我在这里看到了这一点,并一步一步地跟进,但是。。。我不知道哪里是错误。。。所以请帮帮我 这是我的密码:无法在iOS中保存部分屏幕,ios,objective-c,Ios,Objective C,我想截取iphone屏幕的一部分,但我不知道为什么我被卡住了。我不知道我做错了什么,因为我在执行时没有出错。我在这里看到了这一点,并一步一步地跟进,但是。。。我不知道哪里是错误。。。所以请帮帮我 这是我的密码: -(void)saveImageBig { UIImage *myBigImage = [self imageFromBig]; // this method returns a UIImage to store inside myBigImage NSString *
-(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];
}
结果显示图像已成功保存,但图像为空白、空,但该区域中存在对象和控件(大小=568X288)
希望您能帮助我,提前谢谢。您是这样说的:
UIImageView *bigImageView;
[bigImageView.image drawInRect:CGRectMake(16, 20, size.width, size.height)];
因此,在第一行中声明UIImageView。您没有初始化它,因此它为零。在第二行中,您获得此nil图像视图的图像
,即nil。然后将nil绘制到图形上下文中,正如您所说,它是空白的
现在还不清楚你希望用这些废话来完成什么,但你得到的正是我希望你得到的,也就是说,什么都没有。你能想象这些与“保存屏幕的一部分”有什么关系,我都快疯了。您的任何代码都与屏幕上的任何内容无关。我正在修正我的问题,以使问题更加清晰。你的答案是指向正确的答案。