iPhone imageview 打印屏幕 - 从 imageview 创建新图像

发布于 2024-11-01 03:51:19 字数 265 浏览 2 评论 0原文

我可以以某种方式以编程方式打印一个 ImageView 的屏幕吗?

假设我正在显示不同尺寸的大照片,自动缩放(AspectFit)到屏幕尺寸,居中且具有黑色背景。我想捕获缩放图像,包括黑色图像视图背景,以便使用 320x480 图像。

那么如何将 350x600 图像更改为带有黑色边框的 320x480 图像,并且没有任何其他元素(按钮、标签)覆盖此图像视图?在 Android 上,我只是捕获屏幕缓冲区,但对于 iPhone,我真的不知道该怎么做...

提前致谢!

Can I somehow programmatically make a print screen of one ImageView?

Let's say I'm displaying big photos, with different sizes, scaled automatically (AspectFit) to screen size, centered and with black background. I would like to capture the scaled image including the black imageview background to have 320x480 image to work with.

So how to change for example 350x600 image to 320x480 with black border and without any other elements (buttons, labels) overlaying this imageview? On Android I'm just capturing screen buffer but for iPhone I really have no idea how to do it...

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你穿错了嫁妆 2024-11-08 03:51:19
UIGraphicsBeginImageContext(<specify your size here as CGRect>);
[<yourImageView or view you want to convert in image>.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

输出viewImage。由于您想要一个黑色边框,所以您可以有这样的逻辑,请在 UIView 上添加 imageView ,并将 blackBackroundColor 设置为黑色(记住此视图的框架应该大于您的 imageView 的框架)框架的每一侧都有一些边距,以便您可以拥有边框).....现在在 [.layer renderInContext:UIGraphicsGetCurrentContext()]; 使用此方法。

UIGraphicsBeginImageContext(<specify your size here as CGRect>);
[<yourImageView or view you want to convert in image>.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

The viewImage is output. As you want a black border so you can have a logic like this, add you imageView on a UIView with blackBackroundColor as black(remember this view's frame should be greater than your imageView's frame with some margin from each side so that you can have your border)..... and now in [<yourView>.layer renderInContext:UIGraphicsGetCurrentContext()]; use this method.

污味仙女 2024-11-08 03:51:19
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    yourImageViewToStoreCaptured = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

希望这有帮助。

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 480), NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    yourImageViewToStoreCaptured = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

Hope this helps.

孤独患者 2024-11-08 03:51:19
-(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();    
}
-(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();    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文