【问题标题】:How to convert UIView as UIImage?如何将 UIView 转换为 UIImage?
【发布时间】:2011-07-25 01:41:38
【问题描述】:

我正在从 currentview 截取屏幕截图。但我想设置框架……不想将全视图转换为图像。我给了框架大小……但它总是需要 图片为全视图..有什么帮助吗?

            CGRect rectt = CGRectMake(100, 150,150,200);
    UIGraphicsBeginImageContext(rectt.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:rectt];

【问题讨论】:

    标签: iphone ipad ios4 iphone-sdk-3.0 cgcontext


    【解决方案1】:

    尝试使用这个子类:

    // code to use the capture:
        // in .h : #import "CaptureView.h"
        CaptureView *cloneView = [[CaptureView alloc] initWithView:scrollView ];
        [scrollView addSubview:cloneView];
    

    使用此文件代码

    对于 CaptureView.h:

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    #import <QuartzCore/CALayer.h>
    
    
    @interface CaptureView : UIView {
    @private
        UIImage *_imageCapture;
    }
    
    @property(nonatomic, retain) UIImage *imageCapture;
    
    // Init
    - (id)initWithView:(UIView *)view;
    
    @end
    

    对于 CaptureView.m:

    #import "CaptureView.h"
    
    // Private
    @interface CaptureView (/* Private */)
    - (void)settingImageFromView:(UIView *)view;
    @end
    
    // Public
    @implementation CaptureView
    
    @synthesize imageCapture = _imageCapture;
    
    // Standard
    - (id)initWithFrame:(CGRect)frame {
    
        if ((self = [super initWithFrame:frame])) {
            // Initialization code.
        }
        return self;
    }
    
    // Init
    - (id)initWithView:(UIView *)view {
    //    if ((self = [super initWithFrame:[view frame]])) {
        if ((self = [super initWithFrame: CGRectMake(0, 0,150,200)])) {
            // Initialization code.
            [self settingImageFromView:view];
        }
        return self;  
    }
    
    
    - (void)settingImageFromView:(UIView *)view {
    //    CGRect rect = [view bounds];  
        CGRect rect = CGRectMake(100, 150,150,200);  
        UIGraphicsBeginImageContext(rect.size);  
        CGContextRef context = UIGraphicsGetCurrentContext();  
        [view.layer renderInContext:context];  
        UIImage *imageCaptureRect;
    
        imageCaptureRect = UIGraphicsGetImageFromCurrentImageContext();  
        _imageCapture = imageCaptureRect;
    //    _imageCapture = UIGraphicsGetImageFromCurrentImageContext();  
    //    [_imageCapture retain];
        UIGraphicsEndImageContext();   
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code.
        CGPoint accPoint = CGPointMake(0,0);
        [_imageCapture drawAtPoint:accPoint];
    }
    
    
    - (void)dealloc {
        [_imageCapture release];
        [super dealloc];
    }
    
    @end
    

    【讨论】:

    • 这段代码运行良好。一个小的改进是自动读取源 UIView 的宽度和高度: CGRect rect = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    • 这个崩溃就像一个魅力
    【解决方案2】:

    quartz 可能对你有帮助...看看creating image from part of image.... 它不是您问题的直接解决方案,但我认为您可以对其进行调整以满足您的需求..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-09
      • 2012-12-25
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多