如何将UIView转换为UIImage?

发布于 2024-10-27 11:07:04 字数 451 浏览 3 评论 0原文

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

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

I am taking screenshot image from currentview.but i want to set frame….don't want to convert fullview as image.I gave frame size..but it takes always
Image as fullview..any help please?

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

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

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

发布评论

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

评论(2

瘫痪情歌 2024-11-03 11:07:04

尝试使用此子类:

// 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

try to use this subClass:

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

with this files code

for 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

for 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
醉梦枕江山 2024-11-03 11:07:04

石英可能会帮助你...看看从图像的一部分创建图像....
它不是您问题的直接解决方案,但我认为您可以对其进行调整以满足您的需求。

quartz might help you... look at creating image from part of image....
its not a direct solution to your question, but i think you'll be able to tweak it to fit your needs..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文