2012-01-02 22 views
16

जैसा कि आप मेरे कोड में देख सकते हैं, मैं एक स्क्रीनशॉट लेता हूं और इसे फोटो एलबम में सहेजता हूं।प्रोग्रामेटिक रूप से विशिष्ट क्षेत्र का स्क्रीनशॉट लें

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(self.view.bounds.size); 
} 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 

शुरुआत में मैं self.view.bounds.size के बजाय webview.size का इस्तेमाल किया और यह ठीक से काम कर रहा था क्योंकि दृश्य 0/0 पर स्थित था। लेकिन अब मैंने वेब व्यू को केंद्रित किया है लेकिन चित्र दिए गए आकार के लिए 0/0 पर शुरू होते हैं।

मैं कैसे कॉन्फ़िगर कर सकता हूं कि दिए गए आकार के लिए स्क्रीनशॉट किसी अन्य location (उदा। 300/150) से शुरू होता है?

या UIWebView की तस्वीर लेने का कोई और तरीका है?

+0

क्या यह डेस्कटॉप ओएसएक्स या सिर्फ आईफोन पर काम करता है? – Noitidart

उत्तर

2

मैं मेरी समस्या हल लेकिन सवाल का जवाब नहीं दिया:

मैं UIView का एक उपवर्ग बनाया है और चले गए मेरी UIWebView वहाँ में ऐसा है, कि यह subclass के सापेक्ष 0/0 स्थित है। फिर वेबव्यू के आकार का उपयोग करें:

UIGraphicsBeginImageContext(webview.frame.size); 

और ऊपर से अन्य कोड।

अब आप सबक्लास को अपने इच्छित स्थान पर ले जा सकते हैं।

नोट: यदि आपके पास तिथि/समय लेबल जैसे लेबल हैं तो आपको उन्हें स्थानांतरित करना होगा या आप उन्हें तस्वीर पर नहीं देख पाएंगे।

तो UIView का एक उपवर्ग बना सकते हैं और ले जाने के लिए हर IBOutlet आप वहाँ में चित्र पर देखा होना चाहता हूँ।

प्रश्न अभी भी खुला है: तो क्या स्क्रीन के किसी विशिष्ट क्षेत्र की तस्वीर लेना संभव है?

दयालु सम्मान। $ ज @ rky

0
- (void)drawRect:(CGRect)rect { 
    UIGraphicsBeginImageContext(self.bounds.size);  
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
} 

आप अपने दृश्य को यह कह सकते हैं:]

+0

जब वेब दृश्य बनाया जाता है तो drawRect विधि को कॉल किया जाता है - इसलिए मेरे पास शुरुआत में एक भूरे रंग की तस्वीर है। मुझे यह विचार है कि कोड को खींचें और इसे मेरा कस्टम UIView क्लास छोड़ दें जहां मेरा वेब व्यू 0/0 पर स्थित है। काम करता है लेकिन मुझे अब लेबल नहीं दिखाई देता क्योंकि यह "रूट व्यू" का हिस्सा है। – Sharky

23

आप केवल स्क्रीन के एक हिस्से को हड़पने के लिए क्रम में दो बदलाव करने की जरूरत:

  1. एक छोटी छवि बनाएँ - रेक्ट आप पर कब्जा करना चाहते हैं के आकार का उपयोग करें।
  2. उस संदर्भ की उत्पत्ति को ले जाएं जिसे आप उस रेक्ट के नकारात्मक x/y के रूप में प्रस्तुत कर रहे हैं जिसे आप कैप्चर करना चाहते हैं।

उसके बाद, आप बस उस संदर्भ में परत प्रस्तुत करते हैं जैसा आप कर रहे थे, और आपको वह चीज़ मिलनी चाहिए जो आप खोज रहे हैं। ऐसा कुछ ऐसा करना चाहिए:

CGRect grabRect = CGRectMake(40,40,300,200); 

//for retina displays 
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
    UIGraphicsBeginImageContextWithOptions(grabRect.size, NO, [UIScreen mainScreen].scale); 
} else { 
    UIGraphicsBeginImageContext(grabRect.size); 
} 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(ctx, -grabRect.origin.x, -grabRect.origin.y); 
[self.view.layer renderInContext:ctx]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); 
+0

यह मेरे ऐप को दुर्घटनाग्रस्त करता है। कोई विचार क्यों? मैंने क्वार्ट्जकोर फ्रेमवर्क जोड़ा है और इस कोड को फ़ंक्शन में रखा है, लेकिन अभी भी नहीं जाना है। –

+0

@rebellion - मुझे यह काम करने के लिए मिल गया है। हालांकि फोटो एलबम में नहीं। UIView श्रेणी विधि के रूप में नीचे अपना अनुकूलन देखें। – idStar

+0

धन्यवाद, यह मेरे लिए बहुत अच्छा काम करता है। छवि पृष्ठभूमि रंग सफेद है, बस थोड़ा सा समस्या है, मैं इसे पारदर्शी चाहता हूँ। मैं उसे कैसे कर सकता हूँ। कृपया – iMemon

10

मैंने उपरोक्त @ एस्क्राफ़र्ड के उत्तर को लिया और परीक्षण किया है, और इसे ठीक काम करने के लिए मिला है। मैंने इसे UIView पर एक श्रेणी विधि बनाने के संदर्भ में परीक्षण किया था, ताकि 'grabRect' को paramaterized किया जा सके।यहां एक कोड है, जो UIView श्रेणी के रूप में आपको UIImageView देता है:

/** 
Takes a screenshot of a UIView at a specific point and size, as denoted by 
the provided croppingRect parameter. Returns a UIImageView of this cropped 
region. 

CREDIT: This is based on @escrafford's answer at http://stackoverflow.com/a/15304222/535054 
*/ 
- (UIImageView *)rp_screenshotImageViewWithCroppingRect:(CGRect)croppingRect { 
    // For dealing with Retina displays as well as non-Retina, we need to check 
    // the scale factor, if it is available. Note that we use the size of teh cropping Rect 
    // passed in, and not the size of the view we are taking a screenshot of. 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { 
     UIGraphicsBeginImageContextWithOptions(croppingRect.size, YES, [UIScreen mainScreen].scale); 
    } else { 
     UIGraphicsBeginImageContext(croppingRect.size); 
    } 

    // Create a graphics context and translate it the view we want to crop so 
    // that even in grabbing (0,0), that origin point now represents the actual 
    // cropping origin desired: 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(ctx, -croppingRect.origin.x, -croppingRect.origin.y); 
    [self.layer renderInContext:ctx]; 

    // Retrieve a UIImage from the current image context: 
    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    // Return the image in a UIImageView: 
    return [[UIImageView alloc] initWithImage:snapshotImage]; 
} 
+0

यह कोड निश्चित रूप से एआरसी मानता है और वर्तमान (एक कामकाजी समाधान) है जो आईओएस 6.1 पर है। – idStar

+1

महान समाधान! धन्यवाद! – MaKo