天天看點

iOS 獲得指定範圍内的螢幕圖像(截屏)

+ (UIImage *)imageFromView : (UIView *)view

{

    //建立一個基于位圖的圖形上下文并指定大小為CGSizeMake(300,500)

    UIGraphicsBeginImageContext(CGSizeMake(90,90));

    //renderInContext 呈現接受者及其子範圍到指定的上下文

    [view.layerrenderInContext:UIGraphicsGetCurrentContext()];

    //傳回一個基于目前圖形上下文的圖檔

    UIImage *extractImage =UIGraphicsGetImageFromCurrentImageContext();

    //移除棧頂的基于目前位圖的圖形上下文

    UIGraphicsEndImageContext();

    //以png格式傳回指定圖檔的資料

   NSData *imageData = UIImagePNGRepresentation(extractImage);

   UIImage *imge = [UIImageimageWithData:imageData];

   return imge;

}