IPhone फोटो लाइब्रेरी में तस्वीर कैसे बचाएं?


193

आईफोन पर सिस्टम फोटो लाइब्रेरी के लिए मेरे प्रोग्राम द्वारा उत्पन्न की गई छवि (संभवतः कैमरे से, संभवतः नहीं) को बचाने के लिए मुझे क्या करने की आवश्यकता है?


आप इस कोड को देख सकते हैं । भव्य दिवस!
सेलिल बोजर्ट

जवाबों:


411

आप इस फ़ंक्शन का उपयोग कर सकते हैं:

UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                               id completionTarget, 
                               SEL completionSelector, 
                               void *contextInfo);

आप केवल जरूरत completionTarget , completionSelector और contextInfo आप जब सूचना प्राप्त करना चाहते है, तो UIImageबचत से किया जाता है, अन्यथा आप में पारित कर सकते हैं nil

के लिए आधिकारिक दस्तावेजUIImageWriteToSavedPhotosAlbum() देखें ।


सटीक उत्तर के लिए +1 लें
नीरू मुकुंद शाह

हाय आपके महान समाधान के लिए धन्यवाद। यहां मुझे एक संदेह है कि फोटो लाइब्रेरी में छवि को सहेजते समय हम डुप्लिकेट से कैसे बच सकते हैं। अग्रिम में धन्यवाद।
नरेश

अगर आप बेहतर गुणवत्ता में बचत करना चाहते हैं, तो इसे देखें: stackoverflow.com/questions/1379274/…
eonil

4
अब आपको यूजर्स एल्बम को फोटो को सेव करने के लिए iOS 11 के रूप में 'प्राइवेसी - फोटो लाइब्रेरी एडिशन यूसेज डिस्क्रिप्शन' जोड़ना होगा।
घोड़ाजॉकी

1
सहेजे गए चित्रों को नाम कैसे दें?
प्रियाल

63

IOS 9.0 में पदावनत।

वहाँ बहुत तेज है तो UIImageWriteToSavedPhotosAlbum iOS iOS के साथ ऐसा करने का तरीका

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
    // TODO: error handling
    } else {
    // TODO: success handling
    }
}];
[library release];

1
क्या फोटो के साथ-साथ मनमानी मेटाडेटा को बचाने का कोई तरीका है?
१२’१२

2
मैंने उपयोग करके बचत करने की कोशिश की ALAssetsLibrary, इसे सहेजने में बस उतना ही समय लगता है UIImageWriteToSavedPhotosAlbum
१०:५०

और यह कैमरे को मुक्त करता है :( मुझे लगता है कि यह पृष्ठभूमि समर्थित नहीं है?
Hlung

यह एक बहुत क्लीनर बी / सी है जिसे पूरा करने के लिए आप ब्लॉक का उपयोग कर सकते हैं।
jpswain

5
मैं इस कोड का उपयोग कर रहा हूं और मैं इस ढांचे को शामिल कर रहा हूं #import <AssetsLibrary / AssetsLibrary.h> एविएशन नहीं। क्या उत्तर को संपादित नहीं किया जाना चाहिए? @ डेनिस
जूलियन ओसोरियो

30

सबसे सरल तरीका है:

UIImageWriteToSavedPhotosAlbum(myUIImage, nil, nil, nil);

के लिये Swift , आप स्विफ्ट का उपयोग करके आईओएस फोटो लाइब्रेरी में सेविंग का उल्लेख कर सकते हैं


3
मुझे आपका SO उपयोगकर्ता प्रोफ़ाइल आइकन वास्तव में पसंद है। सुंदर शांत Xcode छवि।
सुपेरटेकोनॉफ

1
शानदार सरल, और बहुत आसान!
सेप्ट्रोनिक

13

एक बात याद रखें: यदि आप कॉलबैक का उपयोग करते हैं, तो सुनिश्चित करें कि आपका चयनकर्ता निम्नलिखित फॉर्म के अनुरूप है:

- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo;

अन्यथा, आप निम्न के रूप में एक त्रुटि के साथ क्रैश करेंगे:

[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]


10

बस एक सरणी से छवियों को पास करने के लिए ऐसा है

-(void) saveMePlease {

//Loop through the array here
for (int i=0:i<[arrayOfPhotos count]:i++){
         NSString *file = [arrayOfPhotos objectAtIndex:i];
         NSString *path = [get the path of the image like you would in DOCS FOLDER or whatever];
         NSString *imagePath = [path stringByAppendingString:file];
         UIImage *image = [[[UIImage alloc] initWithContentsOfFile:imagePath]autorelease];

         //Now it will do this for each photo in the array
         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }
}

टाइपो के थोड़े के लिए खेद है कि यह सिर्फ उड़ने पर किया लेकिन आपको यह बात मिल गई


इसके उपयोग से कुछ फ़ोटो छूट जाएँगी, मैंने इसे आज़माया है। इसका सही तरीका पूर्ण चयनकर्ता से कॉल बैक का उपयोग करना है।
सैमचेन

1
हम कस्टम नाम के साथ छवियों को बचा सकते हैं?
यूजर 1531343

इसके लिए कभी भी लूप का उपयोग नहीं करना चाहिए। यह दौड़ की स्थिति और दुर्घटनाओं की ओर जाता है।
saurabh

4

फ़ोटो की एक सरणी सहेजते समय, लूप के लिए उपयोग न करें, निम्न कार्य करें

-(void)saveToAlbum{
   [self performSelectorInBackground:@selector(startSavingToAlbum) withObject:nil];
}
-(void)startSavingToAlbum{
   currentSavingIndex = 0;
   UIImage* img = arrayOfPhoto[currentSavingIndex];//get your image
   UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo{ //can also handle error message as well
   currentSavingIndex ++;
   if (currentSavingIndex >= arrayOfPhoto.count) {
       return; //notify the user it's done.
   }
   else
   {
       UIImage* img = arrayOfPhoto[currentSavingIndex];
       UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
   }
}

4

में स्विफ्ट :

    // Save it to the camera roll / saved photo album
    // UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, nil, nil, nil) or 
    UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, self, "image:didFinishSavingWithError:contextInfo:", nil)

    func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
            if (error != nil) {
                // Something wrong happened.
            } else {
                // Everything is alright.
            }
    }

हां ... अच्छा..लेकिन इमेज सेव करने के बाद मैं गैलरी से इमेज लोड करना चाहता हूं ... कैसे करना है
EI Captain v2.0

4

नीचे फ़ंक्शन काम करेगा। आप यहां से कॉपी करके वहां पेस्ट कर सकते हैं ...

-(void)savePhotoToAlbum:(UIImage*)imageToSave {

    CGImageRef imageRef = imageToSave.CGImage;
    NSDictionary *metadata = [NSDictionary new]; // you can add
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library writeImageToSavedPhotosAlbum:imageRef metadata:metadata completionBlock:^(NSURL *assetURL,NSError *error){
        if(error) {
            NSLog(@"Image save eror");
        }
    }];
}

2

स्विफ्ट 4

func writeImage(image: UIImage) {
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.finishWriteImage), nil)
}

@objc private func finishWriteImage(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
    if (error != nil) {
        // Something wrong happened.
        print("error occurred: \(String(describing: error))")
    } else {
        // Everything is alright.
        print("saved success!")
    }
}

1

मेरा आखिरी जवाब यह करेंगे ..

प्रत्येक छवि जिसे आप सहेजना चाहते हैं, उसे NSMutableArray में जोड़ें

    //in the .h file put:

NSMutableArray *myPhotoArray;


///then in the .m

- (void) viewDidLoad {

 myPhotoArray = [[NSMutableArray alloc]init];



}

//However Your getting images

- (void) someOtherMethod { 

 UIImage *someImage = [your prefered method of using this];
[myPhotoArray addObject:someImage];

}

-(void) saveMePlease {

//Loop through the array here
for (int i=0:i<[myPhotoArray count]:i++){
         NSString *file = [myPhotoArray objectAtIndex:i];
         NSString *path = [get the path of the image like you would in DOCS FOLDER or whatever];
         NSString *imagePath = [path stringByAppendingString:file];
         UIImage *image = [[[UIImage alloc] initWithContentsOfFile:imagePath]autorelease];

         //Now it will do this for each photo in the array
         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        }
}

मैंने आपके समाधान की कोशिश की है, यह हमेशा कुछ तस्वीरों को याद करता है। मेरे जवाब पर एक नजर। लिंक
सैमचेन

1
homeDirectoryPath = NSHomeDirectory();
unexpandedPath = [homeDirectoryPath stringByAppendingString:@"/Pictures/"];

folderPath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];

unexpandedImagePath = [folderPath stringByAppendingString:@"/image.png"];

imagePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedImagePath stringByExpandingTildeInPath]], nil]];

if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:NULL]) {
    [[NSFileManager defaultManager] createDirectoryAtPath:folderPath attributes:nil];
}

यह उत्तर सही नहीं है क्योंकि यह सिस्टम को फोटो लाइब्रेरी के लिए छवि को नहीं बचाता है, लेकिन सैंडबॉक्स को।
इवान

1

मैंने इसके लिए UIImageView श्रेणी बनाई, जो ऊपर दिए गए कुछ उत्तरों पर आधारित है।

शीर्ष लेख फ़ाइल:

@interface UIImageView (SaveImage) <UIActionSheetDelegate>
- (void)addHoldToSave;
@end

कार्यान्वयन

@implementation UIImageView (SaveImage)
- (void)addHoldToSave{
    UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 1.0f;
    [self addGestureRecognizer:longPress];
}

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {

        UIActionSheet* _attachmentMenuSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                          delegate:self
                                                                 cancelButtonTitle:@"Cancel"
                                                            destructiveButtonTitle:nil
                                                                 otherButtonTitles:@"Save Image", nil];
        [_attachmentMenuSheet showInView:[[UIView alloc] initWithFrame:self.frame]];
    }
    else if (sender.state == UIGestureRecognizerStateBegan){
        //Do nothing
    }
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if  (buttonIndex == 0) {
        UIImageWriteToSavedPhotosAlbum(self.image, nil,nil, nil);
    }
}


@end

अब बस इस फंक्शन को अपने इमेजव्यू पर कॉल करें:

[self.imageView addHoldToSave];

वैकल्पिक रूप से आप न्यूनतम टिप पैरामीटर को बदल सकते हैं।


1

में स्विफ्ट 2.2

UIImageWriteToSavedPhotosAlbum(image: UIImage, _ completionTarget: AnyObject?, _ completionSelector: Selector, _ contextInfo: UnsafeMutablePointer<Void>)

आप जब छवि तो बचत किया जाता है यह सूचना प्राप्त नहीं करना चाहते, तो आप में नहीं के बराबर कर सकती है completionTarget , completionSelector और contextInfo मानकों।

उदाहरण:

UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)

func imageSaved(image: UIImage!, didFinishSavingWithError error: NSError?, contextInfo: AnyObject?) {
        if (error != nil) {
            // Something wrong happened.
        } else {
            // Everything is alright.
        }
    }

यहां ध्यान देने वाली महत्वपूर्ण बात यह है कि आपकी विधि जो छवि बचत का अवलोकन करती है, उसमें ये 3 पैरामीटर होने चाहिए अन्यथा आप NSInvocation त्रुटियों में चलेंगे।

आशा करता हूँ की ये काम करेगा।


0

आप इसका उपयोग कर सकते हैं

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   UIImageWriteToSavedPhotosAlbum(img.image, nil, nil, nil);
});
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.