2011-04-13 58 views
1

我可以以某种方式以编程方式制作一个ImageView的打印屏幕吗?iPhone imageview打印屏幕 - 从imageview创建新图像

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

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

在此先感谢!

+0

茹试图让scren拍摄 – 2011-04-13 09:20:13

回答

5
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()];使用这种方法。

+0

关于此边框我的意思在全屏图像视图中缩放图像时的黑色背景。无论哪种方式,当我保存UIImage文件它是白色和空的:( – yosh 2011-04-13 13:12:41

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

希望这会有所帮助。

+0

保存的UIImage * yourImageViewToStoreCaptured在空白8 KB文件磁盘结果:(我捕捉self.imageview.layer。 – yosh 2011-04-13 13:10:18

+0

好了,解决了,THX :) – yosh 2011-04-13 13:39:39

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