ios / Mac view的内容生成image

ios版 

-(UIImage *)getImageViewWithView:(UIView *)view{

     UIGraphicsBeginImageContext(view.frame.size);

     [viewdrawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];

     UIImage*image =  UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();

     returnimage;

}

Mac版

-(NSImage*)getBackViewImage:(NSView*)baview{

    [baviewlockFocus];

    NSImage *image1 = [[NSImage alloc]initWithData:[baview dataWithPDFInsideRect:[baview bounds]]];

    [baviewunlockFocus];

    [image1lockFocus];

    //先设置 下面一个实例

    NSBitmapImageRep *bits1 = [[NSBitmapImageRep alloc]initWithFocusedViewRect:[baview bounds]];//frame

    [image1unlockFocus];

    //    1设置要用到的props属性

    NSDictionary *imageProps1 = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageCompressionFactor];

    //    转化为Data保存

    NSData *imageDat1a = [bits1 representationUsingType:NSPNGFileType properties:imageProps1];

    NSImage*img1 = [[NSImagealloc]initWithData:imageDat1a];

    returnimg1;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。