2012-07-21 10 views

उत्तर

115
बेशक

आप अपने एप्लिकेशन के दस्तावेजों फ़ोल्डर में उप-फ़ोल्डर बना सकते हैं। ऐसा करने के लिए आप का उपयोग करते हैं।

आप UIImagePNGRepresentation का उपयोग NSData करने के लिए अपनी छवि को बदलने और डिस्क के लिए कि बचाने के लिए।

// Create path. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"]; 

// Save image. 
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]; 

कोर डेटा को छवियों को डिस्क पर सहेजने के साथ कुछ भी नहीं करना है।

+0

आप UIImagePNGRepresentation का उपयोग करके सभी उन्मुखीकरण जानकारी खो देते हैं। –

+1

तो मैं जानकारी खोए बिना छवियों को कैसे सहेज सकता हूं? – Pol

3

सबसे पहले आप दस्तावेज़ निर्देशिका मिलना चाहिए

/* create path to cache directory inside the application's Documents directory */ 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"]; 

तो फिर तुम फ़ाइल

NSData *photoData = UIImageJPEGRepresentation(photoImage, 1); 
[photoData writeToFile:filePath atomically:YES]; 
6
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:path atomically:YES]; 

जहां पथ फ़ाइल का नाम आप इसे लिखना चाहते है तस्वीर को बचाने चाहिए सेवा मेरे।

14

ऊपर उपयोगी होते हैं, लेकिन वे कैसे एक उपनिर्देशिका में सहेज लें या उसे UIImagePicker से छवि प्राप्त करने के अपने सवाल का जवाब नहीं है।

@interface CameraViewController() <UIImagePickerControllerDelegate> 

@end 

तो फिर तुम प्रतिनिधि के imagePickerController लागू: didFinishPickingMediaWithInfo: विधि है, जो

सबसे पहले, आप इस तरह के रूप, का उल्लेख करना होगा कि आपके नियंत्रक चित्र पिकर के प्रतिनिधि को लागू करता है, या तो मीटर या ज कोड फ़ाइल में जहाँ आप छवि पिकर से फोटोग्राफ प्राप्त करके सहेज सकते हैं (जाहिर है, यदि आप किसी अन्य वर्ग/उद्देश्य यह है कि बचत संभालती है, लेकिन मैं सिर्फ विधि के अंदर कोड दिखाता हूँ):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    // get the captured image 
    UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage]; 


    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
    NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"]; 

    NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"]; 

    // Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec 
    NSData *imageData = UIImagePNGRepresentation(image); 
    [imageData writeToFile:filePath atomically:YES]; 
} 

हैं आप जेपीईजी छवि, ला के रूप में सहेजना चाहते हैं सेंट 3 लाइनों होगा:

NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"]; 

// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec 
NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85% 
[imageData writeToFile:filePath atomically:YES]; 
25

स्विफ्ट 3 में:

// Create path. 
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
let filePath = "\(paths[0])/MyImageName.png" 

// Save image. 
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true) 
+1

अब मान्य नहीं है - आपको '.write() फेंकता है ' –

9
extension UIImage { 
    /// Save PNG in the Documents directory 
    func save(_ name: String) { 
     let path: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! 
     let url = URL(fileURLWithPath: path).appendingPathComponent(name) 
     try! UIImagePNGRepresentation(self)?.write(to: url) 
     print("saved image at \(url)") 
    } 
} 

// Usage: Saves file in the Documents directory 
image.save("climate_model_2017.png") 
संबंधित मुद्दे