2012-09-09 9 views
5

यहाँ कोड का टुकड़ा मैं करने की कोशिश की है:संपत्ति लाइब्रेरी से किसी छवि विशिष्ट निजी स्थान पर छवि को कैसे स्थानांतरित करें?

[library writeImageToSavedPhotosAlbum:self.checkInImage.CGImage metadata:self.metadata 
      completionBlock    :^(NSURL * assetURL, NSError * error) { 
     NSLog (@"Asset url = %@", assetURL); 
     NSError *err; 
     NSString *docDir = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     NSString *jpgFilePath = [NSString stringWithFormat:@"%@/test.jpg", docDir]; 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
       [fileManager moveItemAtURL:assetURL toURL:[NSURL URLWithString:jpgFilePath] error:&err]; 
       NSLog(@"error is %@", err); 
    }]; 

मैं के रूप में त्रुटि डोमेन = NSCocoaErrorDomain कोड = 262 UserInfo = 0xb49a680 त्रुटि { "कार्रवाई पूर्ण नहीं किया जा सकता है (कोको त्रुटि 262)।" NSURL = संपत्ति-पुस्तकालय: //asset/asset.JPG? Id = 6FAC823A-33CB-489B-A125-714FBA5F0EE8 & ext = JPG}

कोई विचार?

उत्तर

1

यह मैं कैसे समस्या हल हो जाती है।

2) फिर मेटाडेटा के साथ एक ही फ़ाइल को सहेजने के लिए कोड के निम्न भाग का उपयोग किया। यहाँ fileURL urlpath चरण 1 से प्राप्त है

CFURLRef url = CFURLCreateFilePathURL(NULL, (CFURLRef)fileURL, NULL); 
// Creating Image source from image url 
CGImageSourceRef imageSource = CGImageSourceCreateWithURL(url, NULL); 

// This will be the data CGImageDestinationRef will write into 
CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); 
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithData(data, CFSTR("public.jpeg"), 1, NULL); 

BOOL success = (imageSource != NULL); 

require(success, cleanup); 

success = (imageDestination != NULL); 
require(success, cleanup); 

// Add the image contained in the image source to the destination, overidding the old metadata with our modified metadata 
NSDictionary *metadata = someObj.metadata; 
CGImageDestinationAddImageFromSource(imageDestination, imageSource, 0, (CFDictionaryRef)metadata); 

// Tell the destination to write the image data and metadata into our data object. 
success = CGImageDestinationFinalize(imageDestination); 

require(success, cleanup); 

// Write the data into the url 
[(NSMutableData *) data writeToURL:fileURL atomically:YES]; 

सफाई:

if (data) { 
    CFRelease(data); 
    data = NULL; 
} 

if (imageSource) { 
    CFRelease(imageSource); 
    imageSource = NULL; 
} 

if (imageDestination) { 
    CFRelease(imageDestination); 
    imageDestination = NULL; 
} 

return success; 

आशा इस मदद करता है। लेकिन मैं बहुत उपयोगी सुझावों के लिए H2C03 को ऊपर उठाना चाहता हूं, जिसने इस समाधान पर पहुंचने में मदद की। कृपया मेरी मदद करें कि इसे कैसे करें। धन्यवाद।

5

Docs (and Google). यह त्रुटि कोड NSFileReadUnsupportedSchemeError स्थिरता से मेल खाता है - i। ई। आप NSFileManager का उपयोग कर फ़ाइल को स्थानांतरित करने के लिए केवल assets-library:// URL का उपयोग नहीं कर सकते हैं। (ipod-library:// यूआरएल के मामले में भी यही मामला है।) फ़ाइल के डेटा प्राप्त करने के लिए आपको एसेट्स लाइब्रेरी फ्रेमवर्क का उपयोग करना होगा और फिर इसे - [NSData writeToFile:atomically:] का उपयोग करके फ़ाइल में लिखें।

1) मैं कुछ स्थान में मेटाडाटा के बिना फाइल सेव, और इसके urlpath संग्रहीत:

ALAssetsLibrary *lib = [ALAssetsLibrary new]; 
[lib assetForUrl:theURL // the asset URL you have obtained, NSURL object 
    resultBlock:^(ALAsset *asset) { 
     // get data 
     ALAssetRepresentation *repr = [asset defaultRepresentation]; 
     CGImageRef cgImg = [repr fullResolutionImage]; 
     NSString *fname = [repr fileName]; 
     UIImage *img = [UIImage imageWithCGImage:cgImg]; 
     NSData *data = UIImagePNGRepresentation(img); 
     [data writeToFile:[@"BaseDirectory/" stringByAppendingPathComponent:fname] 
      atomically:YES]; 
     [lib release]; 
    } 
    failureBlock:^(NSError *error) { 
     // recover from error, then 
     [lib release]; 
    }]; 
+0

धन्यवाद मैं कोशिश करूँगा। आदर्श रूप से मैं फ़ाइल को स्थानांतरित करना चाहता हूं, इसका मतलब है कि फ़ाइल कॉपी करें और संपत्तियों में से एक को हटा दें। क्या यह संभव है? – mobjug

+0

@mobjug बिल्कुल नहीं। आप सीधे किसी गीत के फाइल सिस्टम स्थान तक नहीं पहुंच सकते हैं, न ही आप आईपॉड लाइब्रेरी में कुछ भी संशोधित कर सकते हैं (ठीक है, मैं उस चीज़ पर काम कर रहा हूं जिसके साथ यह संभव होगा, लेकिन मेरा यह प्रोजेक्ट तैयार होने के करीब भी नहीं है ...) –

+0

भले ही मेरे ऐप ने उस फाइल को बनाया हो? – mobjug

संबंधित मुद्दे