जवाबों:
आप इस फ़ंक्शन का उपयोग कर सकते हैं:
UIImageWriteToSavedPhotosAlbum(UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo);
आप केवल जरूरत completionTarget , completionSelector और contextInfo आप जब सूचना प्राप्त करना चाहते है, तो UIImage
बचत से किया जाता है, अन्यथा आप में पारित कर सकते हैं nil
।
के लिए आधिकारिक दस्तावेजUIImageWriteToSavedPhotosAlbum()
देखें ।
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];
ALAssetsLibrary
, इसे सहेजने में बस उतना ही समय लगता है UIImageWriteToSavedPhotosAlbum
।
सबसे सरल तरीका है:
UIImageWriteToSavedPhotosAlbum(myUIImage, nil, nil, nil);
के लिये Swift
, आप स्विफ्ट का उपयोग करके आईओएस फोटो लाइब्रेरी में सेविंग का उल्लेख कर सकते हैं
एक बात याद रखें: यदि आप कॉलबैक का उपयोग करते हैं, तो सुनिश्चित करें कि आपका चयनकर्ता निम्नलिखित फॉर्म के अनुरूप है:
- (void) image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo;
अन्यथा, आप निम्न के रूप में एक त्रुटि के साथ क्रैश करेंगे:
[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]
बस एक सरणी से छवियों को पास करने के लिए ऐसा है
-(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);
}
}
टाइपो के थोड़े के लिए खेद है कि यह सिर्फ उड़ने पर किया लेकिन आपको यह बात मिल गई
फ़ोटो की एक सरणी सहेजते समय, लूप के लिए उपयोग न करें, निम्न कार्य करें
-(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);
}
}
में स्विफ्ट :
// 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.
}
}
नीचे फ़ंक्शन काम करेगा। आप यहां से कॉपी करके वहां पेस्ट कर सकते हैं ...
-(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");
}
}];
}
स्विफ्ट 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!")
}
}
मेरा आखिरी जवाब यह करेंगे ..
प्रत्येक छवि जिसे आप सहेजना चाहते हैं, उसे 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);
}
}
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];
}
मैंने इसके लिए 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];
वैकल्पिक रूप से आप न्यूनतम टिप पैरामीटर को बदल सकते हैं।
में स्विफ्ट 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 त्रुटियों में चलेंगे।
आशा करता हूँ की ये काम करेगा।
आप इसका उपयोग कर सकते हैं
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImageWriteToSavedPhotosAlbum(img.image, nil, nil, nil);
});