2012-09-09 32 views
5

Dưới đây là đoạn mã tôi đã cố gắng:Cách di chuyển hình ảnh từ thư viện nội dung sang một vị trí riêng tư của ứng dụng?

[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); 
    }]; 

tôi nhận được lỗi như Lỗi miền = Mã NSCocoaErrorDomain = 262 UserInfo = 0xb49a680 { "Các hoạt động không thể hoàn tất (Cocoa lỗi 262.)." NSURL = asset-library: //asset/asset.JPG? Id = 6FAC823A-33CB-489B-A125-714FBA5F0EE8 & ext = JPG}

Bất kỳ ý tưởng nào?

Trả lời

1

Đây là cách tôi giải quyết vấn đề này:

1) Tôi đã lưu file mà không cần siêu dữ liệu ở một số vị trí và lưu trữ urlpath của nó.

2) Sau đó sử dụng đoạn mã sau đây để lưu cùng một tệp với siêu dữ liệu. Dưới đây fileURL là urlpath thu được từ bước 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]; 

dọn dẹp:

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

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

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

return success; 

Hope this helps. Nhưng tôi muốn upvote H2C03 cho những gợi ý rất hữu ích, đã giúp để đi đến giải pháp này. Xin hãy giúp tôi làm thế nào để làm điều đó. Cảm ơn.

5

Docs (and Google). Mã lỗi này tương ứng với hằng số NSFileReadUnsupportedSchemeError - i. e. bạn không thể chỉ sử dụng URL assets-library:// để di chuyển tệp bằng NSFileManager. (Cũng giống như trường hợp liên quan đến các URL ipod-library://.) Bạn phải sử dụng khung AssetsLibrary để có được dữ liệu của tệp và sau đó ghi tệp đó vào tệp bằng cách sử dụng - [NSData writeToFile:atomically:].

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

Cảm ơn tôi sẽ thử điều đó. Lý tưởng nhất là tôi muốn di chuyển tệp, điều đó có nghĩa là sao chép tệp và xóa tệp đó trong nội dung. Có thể không? – mobjug

+0

@mobjug không hề. Bạn không thể truy cập trực tiếp vào vị trí hệ thống tập tin của một bài hát, cũng như bạn có thể sửa đổi bất cứ thứ gì trong thư viện iPod (tốt, tôi đang làm việc với một thứ gì đó mà nó có thể thực hiện được). ...) –

+0

Ngay cả khi ứng dụng của tôi đã tạo tệp đó? – mobjug

Các vấn đề liên quan