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;

}

你可能感兴趣的:(ios / Mac view的内容生成image)