我可以以某种方式以编程方式制作一个 ImageView 的打印屏幕吗?
假设我正在显示不同尺寸的大照片,自动缩放(AspectFit)到屏幕尺寸,居中并带有黑色背景。我想捕获包含黑色图像视图背景的缩放图像,以使用 320x480 图像。
那么如何将例如 350x600 图像更改为 320x480 并带有黑色边框并且没有任何其他元素(按钮、标签)覆盖此图像视图?在 Android 上,我只是在捕获屏幕缓冲区,但对于 iPhone,我真的不知道该怎么做......
提前致谢!
我可以以某种方式以编程方式制作一个 ImageView 的打印屏幕吗?
假设我正在显示不同尺寸的大照片,自动缩放(AspectFit)到屏幕尺寸,居中并带有黑色背景。我想捕获包含黑色图像视图背景的缩放图像,以使用 320x480 图像。
那么如何将例如 350x600 图像更改为 320x480 并带有黑色边框并且没有任何其他元素(按钮、标签)覆盖此图像视图?在 Android 上,我只是在捕获屏幕缓冲区,但对于 iPhone,我真的不知道该怎么做......
提前致谢!
UIGraphicsBeginImageContext(<specify your size here as CGRect>);
[<yourImageView or view you want to convert in image>.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
是viewImage
输出。因为你想要一个黑色边框,所以你可以有这样的逻辑,把你添加imageView
到一个黑色的 UIView 上blackBackroundColor
(记住这个视图的框架应该大于你的 imageView 的框架,每边都有一些边距,这样你就可以有你的边框)。 ....现在正在 [<yourView>.layer renderInContext:UIGraphicsGetCurrentContext()];
使用这种方法。
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();
}