2015-08-15 15 views
6

मैं पूरे स्क्रीन, screenshot का एक स्क्रीनशॉट है, उत्पन्न का उपयोग कर निम्न:स्विफ्ट: फसल एक स्क्रीनशॉट

let layer = UIApplication.sharedApplication().keyWindow!.layer 
let scale = UIScreen.mainScreen().scale 
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); 

layer.renderInContext(UIGraphicsGetCurrentContext()) 
let screenshot = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

ताकि टैब बार शामिल नहीं है मैं इसे काटने के लिए चाहते हैं, और मैंने कोशिश की

let crop = CGRectMake(0, 0, //"start" at the upper-left corner 
self.view.bounds.width, //include half the width of the whole screen 
self.view.bounds.height + self.navigationController!.navigationBar.frame.height) //include the height of the navigationBar and the height of view 

let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop) 
let image: UIImage = UIImage(CGImage: cgImage)! 

सही से भी कम समय के लिए इस कोड को image में परिणाम स्क्रीन के केवल एक छोटा सा हिस्सा दिखा रहा है, एक आयत स्क्रीन के ऊपरी-बाएं कोने पर शुरू (0, 0), और विस्तार: निम्नलिखित कोड का उपयोग कर स्क्रीन की आधा चौड़ाई, फिर हेक्टेयर से कम के लिए नीचे की ओर स्क्रीन की ऊंचाई एलएफ। हालांकि, टैब टैब द्वारा कब्जे वाले क्षेत्र को छोड़कर, मुझे संपूर्ण स्क्रीन शामिल करना होगा। क्या इसे फसल करने का कोई तरीका है?

उत्तर

8

इस

के अनुसार नई छवि
1) CGRectIntegral फोन करके अभिन्न सीमा को rect का समायोजन करके बनाई गई है;
2) image के आकार के बराबर मूल (0, 0) और आकार के साथ आयताकार के साथ परिणाम को छेड़छाड़ करना;
3) परिणामी आयताकार के भीतर पिक्सल का संदर्भ, छवि डेटा की पहली पिक्सेल छवि के मूल के रूप में इलाज।
यदि परिणामी आयताकार शून्य आयताकार है, तो यह फ़ंक्शन न्यूल देता है।

यदि डब्ल्यू और एच क्रमशः छवि की चौड़ाई और ऊंचाई हैं, तो बिंदु (0,0) छवि डेटा के पहले पिक्सेल से मेल खाता है; बिंदु (डब्ल्यू -1, 0) छवि डेटा की पहली पंक्ति का अंतिम पिक्सेल है; (0, एच -1) छवि डेटा की अंतिम पंक्ति का पहला पिक्सेल है; और (डब्ल्यू -1, एच -1) छवि डेटा की अंतिम पंक्ति का अंतिम पिक्सेल है।

आपको इस तरह एक फसल कार्य करने की आवश्यकता होगी। आप bottomBarHeight

func takeScreenshot(sender: AnyObject) { 
    let layer = UIApplication.sharedApplication().keyWindow!.layer 
    let scale = UIScreen.mainScreen().scale 
    UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale); 

    layer.renderInContext(UIGraphicsGetCurrentContext()) 
    let screenshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    let croppedImage = self.cropImage(screenshot) 
} 

func cropImage(screenshot: UIImage) -> UIImage { 
    let scale = screenshot.scale 
    let imgSize = screenshot.size 
    let screenHeight = UIScreen.mainScreen().applicationFrame.height 
    let bound = self.view.bounds.height 
    let navHeight = self.navigationController!.navigationBar.frame.height 
    let bottomBarHeight = screenHeight - navHeight - bound 
    let crop = CGRectMake(0, 0, //"start" at the upper-left corner 
     (imgSize.width - 1) * scale, //include half the width of the whole screen 
     (imgSize.height - bottomBarHeight - 1) * scale) //include the height of the navigationBar and the height of view 

    let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop) 
    let image: UIImage = UIImage(CGImage: cgImage)! 
    return image 
} 
+0

पूरी तरह से काम करता है-धन्यवाद; बक्षीस आना है। – Randoms

+0

आपका स्वागत है :) – sahara108

3

तेज:

let contextImage: UIImage = <<screenshot>>! 
let cropRect: CGRect = CGRectMake(x, y, width, height) 
let imageRef: CGImageRef = CGImageCreateWithImageInRect(contextImage.CGImage, cropRect) 
let image: UIImage = UIImage(CGImage: imageRef, scale: originalImage.scale, orientation: originalImage.imageOrientation)! 

Obj सी:

UIImage *image = <<screenshot>>; 

CGRect croprect = CGRectMake(0, 0, 
self.view.bounds.width, 
self.view.bounds.height + self.navigationController!.navigationBar.frame.height)); 

// Draw new image in current graphics context 
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], croprect); 

// Create new cropped UIImage 
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; 
+0

की गणना यह सवाल स्विफ्ट, नहीं ऑब्जेक्टिव-सी में एक जवाब की तलाश में है समायोजित पड़ सकता है। – JAL

+0

मेरा बुरा ... मैंने तेजी से जोड़ा –

1

आपके पास दो विकल्प यहाँ हैं:

UIGraphicsBeginImageContext(UIApplication.sharedApplication().keyWindow!.bounds.size) 
    UIApplication.sharedApplication().keyWindow!.layer.renderInContext(UIGraphicsGetCurrentContext()) 
    let screenshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
: सबसे पहले, आप UIApplication.sharedApplication().keyWindow का उपयोग कर तो हम इस होगा स्क्रीनशॉट ले सकते हैं

तो, आपको इस तरह की छवि को फसल करना होगा:

let yPosition = self.navigationController!.navigationBar.frame.height + UIApplication.sharedApplication().statusBarFrame.size.height 
    let crop = CGRectMake(0, yPosition, 
      self.view.bounds.width, 
      self.view.bounds.height) 
    let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop) 
    let image: UIImage = UIImage(CGImage: cgImage)! 

दूसरा विकल्प इस तरह आपके विचार के सीधे स्क्रीनशॉट लेने के लिए, यह है:

UIGraphicsBeginImageContext(self.view!.bounds.size) 
    self.view!.layer.renderInContext(UIGraphicsGetCurrentContext()) 
    let screenshot = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

इस मामले में, आप navigationBar फसल की जरूरत नहीं है।

यह GitHub https://github.com/gazolla/crop

+0

छवि को फसल करने के लिए आपका सुझाव मुझे जो भी चाहिए, पूरा नहीं कर रहा है-यह अभी भी स्क्रीन के ऊपरी-बाएं सेक्शन का एक छोटा सा हिस्सा है। – Randoms

+0

इस नमूना कोड को github https://github.com/gazolla/crop – Sebastian

+0

पर आज़माएं जो नेविगेशन बार को फसल करता है, न कि टैब बार। – Randoms

0

पर एक नमूना कोड मुझे लगता है कि अपने फसल CGRect गलत है।

let crop = CGRectMake(0, 
         0, 
         self.view.frame.size.width, 
         self.view.frame.size.height + self.navigationController!.navigationBar.frame.height - self.tabBar.frame.size.height 
        ) 

एक्स: 0 और वाई: 0 - क्रमशः 0, 0 से शुरू करें।

चौड़ाई: दृश्य के पूरे चौड़ाई कैद

ऊंचाई: पूरे दृश्य की ऊंचाई से अधिक UINavigationBar की ऊंचाई शून्य से UITabBar की ऊंचाई।

+0

क्षमा करें यह अभी भी मेरे लिए काम नहीं कर रहा है, मैं अभी भी ऊपरी-बाएं चतुर्भुज को देखता हूं। क्या यह संभवतः उस तरह के प्रदर्शन के साथ कुछ करना है जिसके साथ मैं काम कर रहा हूं (एक आईफोन 6 प्लस)? – Randoms

+0

आप इस कोड को कहां कॉल कर रहे हैं? एक दृश्य नियंत्रक? एक सबव्यू? – JAL

+0

एक व्यू कंट्रोलर, विशेष रूप से एक नेविबार बटन की क्रिया। – Randoms

संबंधित मुद्दे