मैं चाहता हूं कि उपयोगकर्ता मेरे ऐप पर जाएं और स्विफ्ट में प्रोग्रामेटिक रूप से बटन दबाए जाने के बाद ऐप का स्क्रीनशॉट लें। मुझे पता है कि UIGraphicsGetImageFromCurrentImageContext()
एक स्क्रीनशॉट लेता है लेकिन मुझे पूरी स्क्रीन की एक तस्वीर नहीं चाहिए। मैं एक आयताकार पॉप अप करना चाहता हूं (एक फसल टूल की तरह) और उपयोगकर्ता स्क्रीन के केवल एक निश्चित भाग का स्क्रीनशॉट लेने के लिए आयत खींच और आकार बदल सकता है। मैं आयताकार को WKWebView
पर जाना चाहता हूं और वेब दृश्य की एक तस्वीर फसल करना चाहता हूं।UIView के हिस्से का स्क्रीनशॉट कैसे लें?
6
A
उत्तर
16
मानक स्नैपशॉट तकनीक drawViewHierarchyInRect
है। और एक छवि का हिस्सा पाने के लिए, आप CGImageCreateWithImageInRect
का उपयोग कर सकते हैं।
इस प्रकार, स्विफ्ट 2 में:
extension UIView {
/// Create snapshot
///
/// - parameter rect: The `CGRect` of the portion of the view to return. If `nil` (or omitted),
/// return snapshot of the whole view.
///
/// - returns: Returns `UIImage` of the specified portion of the view.
func snapshot(of rect: CGRect? = nil) -> UIImage? {
// snapshot entire view
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, 0)
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// if no `rect` provided, return image of whole view
guard let rect = rect, let image = wholeImage else { return wholeImage }
// otherwise, grab specified `rect` of image
let scale = image.scale
let scaledRect = CGRect(x: rect.origin.x * scale, y: rect.origin.y * scale, width: rect.size.width * scale, height: rect.size.height * scale)
guard let cgImage = CGImageCreateWithImageInRect(image.CGImage!, scaledRect) else { return nil }
return UIImage(CGImage: cgImage, scale: scale, orientation: .Up)
}
}
स्विफ्ट 3 में:
extension UIView {
/// Create snapshot
///
/// - parameter rect: The `CGRect` of the portion of the view to return. If `nil` (or omitted),
/// return snapshot of the whole view.
///
/// - returns: Returns `UIImage` of the specified portion of the view.
func snapshot(of rect: CGRect? = nil) -> UIImage? {
// snapshot entire view
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
drawHierarchy(in: bounds, afterScreenUpdates: true)
let wholeImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// if no `rect` provided, return image of whole view
guard let image = wholeImage, let rect = rect else { return wholeImage }
// otherwise, grab specified `rect` of image
let scale = image.scale
let scaledRect = CGRect(x: rect.origin.x * scale, y: rect.origin.y * scale, width: rect.size.width * scale, height: rect.size.height * scale)
guard let cgImage = image.cgImage?.cropping(to: scaledRect) else { return nil }
return UIImage(cgImage: cgImage, scale: scale, orientation: .up)
}
}
और यह उपयोग करने के लिए, आप कर सकते हैं:
if let image = webView.snapshot(of: rect) {
// do something with `image` here
}
3
यह How to capture UIView to UIImage without loss of quality on retina display पर एक पहले से पूछा सवाल, लेकिन तेज में विस्तार करने के लिए (2.3) है:
extension UIView {
class func image(view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
guard let ctx = UIGraphicsGetCurrentContext() else {
return nil
}
view.layer.renderInContext(ctx)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
func image() -> UIImage? {
return UIView.image(self)
}
}
तो आप कर सकते हैं या तो UIView.image(theView)
साथ या देखने में ही let viewImage = self.view.image()
ध्यान रखें कि यह मोटा है और शायद थ्रेड सुरक्षा आदि के लिए आगे की जरूरत है ....
संबंधित मुद्दे
- 1. UIScrollView दृश्य क्षेत्र का स्क्रीनशॉट कैसे लें?
- 2. स्क्रीनशॉट लें
- 3. प्रोग्रामशेट स्क्रीनशॉट कैसे लें?
- 4. एचटीएमएल-यूनिट के साथ स्क्रीनशॉट कैसे लें?
- 5. वेलैंड के तहत स्क्रीनशॉट कैसे लें?
- 6. पूरे दृश्य का एक स्क्रीनशॉट लें
- 7. एंड्रॉइड चीजें: एक स्क्रीनशॉट लें
- 8. प्रस्तुत किए गए HTML पृष्ठ का स्क्रीनशॉट कैसे लें
- 9. आईफोन के बिना, एक्सकोड के साथ स्क्रीनशॉट कैसे लें?
- 10. जावास्क्रिप्ट के साथ एक वेबपृष्ठ का एक स्क्रीनशॉट लें?
- 11. फ़ायरफ़ॉक्स एडन एसडीके 1.17 स्क्रीनशॉट लें
- 12. xlib के साथ स्क्रीनशॉट को सही तरीके से कैसे लें?
- 13. बिना रूट के एंड्रॉइड 2.3.3 में स्क्रीनशॉट कैसे लें?
- 14. पायथन में स्क्रीनशॉट लें - क्रॉस प्लेटफ़ॉर्म
- 15. प्रोग्रामेटिक रूप से विशिष्ट क्षेत्र का स्क्रीनशॉट लें
- 16. विंडोज लॉक होने पर डेस्कटॉप का स्क्रीनशॉट लें (विन + एल)
- 17. QWidget का स्क्रीनशॉट कैसे बनाएं?
- 18. एक पायथन लिपि के माध्यम से एक स्क्रीनशॉट लें। [लिनक्स]
- 19. चांदी की रोशनी के साथ एक स्क्रीनशॉट लें 5/xna
- 20. सी # आवेदन के भीतर .NET नियंत्रण का स्क्रीनशॉट लें और Outlook ईमेल
- 21. एचटीएमएल 5/जावास्क्रिप्ट का उपयोग कर पूरी स्क्रीन का एक स्क्रीनशॉट लें?
- 22. Xamarin.iOS (सी #) के साथ प्रोग्रामिक रूप से आईफोन/आईपैड का स्क्रीनशॉट कैसे लें?
- 23. ब्लैकबेरी के लिए स्क्रीनशॉट उपकरण?
- 24. प्रोग्रामेटिक रूप से ओपनजीएल और UIKit तत्वों का संयोजन स्क्रीनशॉट लें
- 25. स्क्रीनशॉट प्रोग्रामेटिक रूप से कैसे लें और इसे गैलरी पर कैसे सहेजें?
- 26. एचटीएमएल 2 कैनवास के साथ <body> के स्क्रीनशॉट लें और जेएस var
- 27. एक यूआईवीव का एक स्क्रीनशॉट लें जहां इसके सबव्यू कैमरे सत्र हैं
- 28. स्क्रीनशॉट
- 29. स्क्रीनशॉट
- 30. वर्तमान में वीडियो चलाने का स्क्रीन शॉट कैसे लें?
महान काम हमेशा की तरह रोब। धन्यवाद। – damirstuhec
बिल्कुल सही समाधान। एसओ पर अन्य लोग हैं लेकिन वे पूरी स्क्रीन पर कब्जा करते हैं। यह दोनों के लिए समाधान है। – Septronic