UIView+Image

#import <UIKit/UIKit.h>

@interface UIView (Image)

- (UIImage *) imageByRenderingView;

@end

#import "UIView+Image.h"
#include <QuartzCore/QuartzCore.h>

@implementation UIView (Image)

- (UIImage *)imageByRenderingView
{   
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
    
//    CGImageRef topOfImageCG =
//    CGImageCreateWithImageInRect(currImage.CGImage,
//                                 CGRectMake(0,
//                                            0,
//                                            currImage.size.width,
//                                            currImage.size.height / 2.0));
//    
//    UIImage *topOfImage = [UIImage imageWithCGImage:topOfImageCG];
//    
//    CGImageRelease(topOfImageCG);

}

@end

 将view转换成image的一个category, 注释的部分是截取一半图片的方法。

posted @ 2011-12-30 15:12  周宏伟  阅读(627)  评论(0编辑  收藏  举报