首页 文章

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

提问于
浏览
1

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

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

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

提前致谢!

3 回答

  • 1
    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上添加 imageViewblackBackroundColor 为黑色(请记住这个视图's frame should be greater than your imageView'的框架,每边有一些边距,这样你就可以拥有你的边框).... . 现在 [<yourView>.layer renderInContext:UIGraphicsGetCurrentContext()]; 使用这种方法 .

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

    希望这可以帮助 .

  • 5
    -(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();    
    }
    

相关问题