iPhone imageview打印屏幕-从imageview创建新图像
我是否可以通过编程方式为一个ImageView创建打印屏幕 比如说,我正在显示大照片,大小不同,自动缩放(AspectFit)到屏幕大小,居中,背景为黑色。我想捕获缩放图像,包括黑色imageview背景,以便使用320x480图像 那么,如何将例如350x600图像更改为320x480,带有黑色边框,并且没有覆盖此图像视图的任何其他元素(按钮、标签)?在Android上,我只是捕捉屏幕缓冲区,但在iPhone上,我真的不知道怎么做 提前谢谢iPhone imageview打印屏幕-从imageview创建新图像,iphone,cocoa-touch,image,imageview,screen-capture,Iphone,Cocoa Touch,Image,Imageview,Screen Capture,我是否可以通过编程方式为一个ImageView创建打印屏幕 比如说,我正在显示大照片,大小不同,自动缩放(AspectFit)到屏幕大小,居中,背景为黑色。我想捕获缩放图像,包括黑色imageview背景,以便使用320x480图像 那么,如何将例如350x600图像更改为320x480,带有黑色边框,并且没有覆盖此图像视图的任何其他元素(按钮、标签)?在Android上,我只是捕捉屏幕缓冲区,但在iPhone上,我真的不知道怎么做 提前谢谢 UIGraphicsBeginImageCo
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
yourImageViewToStoreCaptured = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
希望这有帮助
-(IBAction)saveViewToPhotoLibrary:(id)sender
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenImage,nil,nil,nil);
//fill cordinat in place of nil if u want only image comes and not button......ok thanks vijay//
UIGraphicsEndImageContext();
}
希望这有帮助 UIGraphicsBeginImageContext();
-(IBAction)saveViewToPhotoLibrary:(id)sender
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(screenImage,nil,nil,nil);
//fill cordinat in place of nil if u want only image comes and not button......ok thanks vijay//
UIGraphicsEndImageContext();
}
[.layer RenderContext:UIGraphicsGetCurrentContext()];
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
将输出视图图像
。由于您需要黑色边框,因此可以使用这样的逻辑,请将您的imageView
添加到一个UIView上,并将blackBackroundColor
设置为黑色(请记住,此视图的边框应该比您的imageView的边框大,每侧都有一些边距,以便您可以使用边框)。。。。。现在在[.layer renderInContext:UIGraphicsGetCurrentContext()]中代码>使用此方法。UIGraphicsBeginImageContext();
[.layer RenderContext:UIGraphicsGetCurrentContext()];
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
将输出视图图像
。由于您需要黑色边框,因此可以使用这样的逻辑,请将您的imageView
添加到一个UIView上,并将blackBackroundColor
设置为黑色(请记住,此视图的边框应该比您的imageView的边框大,每侧都有一些边距,以便您可以使用边框)。。。。。现在在[.layer renderInContext:UIGraphicsGetCurrentContext()]中代码>使用此方法。将UIImage*yourImageViewToStoreCaptured保存到磁盘会导致空白的8 kb文件:(我正在捕获self.imageview.layer.Save UIImage*yourImageViewToStoreCaptured到磁盘会导致空白的8 kb文件:(我正在捕获self.imageview.layer。关于此边框,我只是指在全屏imageview中缩放图像时的黑色背景。无论哪种方式,当我将UIImage保存到文件时,它是白色且为空的:(关于此边框,我指的是在全屏图像视图中缩放图像时的黑色背景。无论哪种方式,当我将UIImage保存到文件时,它都是白色且为空的:(