如何通过ALAssetsLibrary将所选图像从库中保存到应用程序的文档目录

时间:2016-04-19 06:45:22

标签: ios

我已经通过ALAssetsLibrary从图库到集合视图中提取了所有图像。我已经成功地完成了它。

现在,我想将所选图像从集合视图保存到应用程序的文档目录。

通过此代码选择图像 -

-(void)done
{
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:[selectedIndexArr count]];

[selectedIndexArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:obj resultBlock:^(ALAsset *asset) {
        ALAssetRepresentation *representation = [asset defaultRepresentation];
        UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
        [imageArray addObject:latestPhoto];

        if ([imageArray count]==[selectedIndexArr count]) {
            if ([self.delegate respondsToSelector:@selector(ims_PickerController:didFinishPickingMediaItems:)]) {
                [self.delegate ims_PickerController:self didFinishPickingMediaItems:imageArray];
            }
        }
        [self saveImagesFromLibraryToDocumentDirectory:imageArray];
    } failureBlock:^(NSError *error) {
        NSLog(@"Failure picking media");
    }];
}];
 }

当我尝试将其保存在文档目录中时,这两个都正常工作我在这行收到错误:

 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString* filename = [docPath stringByAppendingPathComponent:[representation filename]];

由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [UIImage defaultRepresentation]:无法识别的选择器发送到实例0x7afe6150' ***第一次抛出调用堆栈: (

0   CoreFoundation                      0x01a28946 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x016b1a97 objc_exception_throw + 44
2   CoreFoundation                      0x01a305c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x019793e7 ___forwarding___ + 1047
4   CoreFoundation                      0x01978fae _CF_forwarding_prep_0 + 14
5   MovieMixture1                       0x00067571 -[ImageSelector saveImagesFromLibraryToDocumentDirectory:] + 177
6   MovieMixture1                       0x000671b1 __21-[ImageSelector done]_block_invoke_2 + 513
7   AssetsLibrary                       0x0013a216 __56-[ALAssetsLibrary assetForURL:resultBlock:failureBlock:]_block_invoke_2 + 215
8   libdispatch.dylib                   0x056eb30a _dispatch_call_block_and_release + 15
9   libdispatch.dylib                   0x0570be2f _dispatch_client_callout + 14
10  libdispatch.dylib                   0x056f290e _dispatch_main_queue_callback_4CF + 606
11  CoreFoundation                      0x0198295e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
12  CoreFoundation                      0x01941760 __CFRunLoopRun + 2256
13  CoreFoundation                      0x01940bcb CFRunLoopRunSpecific + 443
14  CoreFoundation                      0x019409fb CFRunLoopRunInMode + 123
15  GraphicsServices                    0x04b3324f GSEventRunModal + 192
16  GraphicsServices                    0x04b3308c GSEventRun + 104
17  UIKit                               0x020198b6 UIApplicationMain + 1526
18  MovieMixture1                       0x0007dc4d main + 141
19  libdyld.dylib                       0x05737ac9 start + 1

请帮我解决这个问题。

我保存图片的代码:

-(void)saveImagesFromLibraryToDocumentDirectory:(NSArray *)imageArray
{
for (int j=0; j<[imageArray count]; j++) {

    ALAssetRepresentation *representation = [[imageArray objectAtIndex:j] defaultRepresentation];

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString* filename = [docPath stringByAppendingPathComponent:[representation filename]];

    [[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
    NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:filename append:YES];
    [outPutStream open];

    long long offset = 0;
    long long bytesRead = 0;

    NSError *error;
    uint8_t * buffer = malloc(131072);
    while (offset<[representation size] && [outPutStream hasSpaceAvailable]) {
        bytesRead = [representation getBytes:buffer fromOffset:offset length:131072 error:&error];
        [outPutStream write:buffer maxLength:bytesRead];
        offset = offset+bytesRead;
    }
    [outPutStream close];
    free(buffer);
}
}

0 个答案:

没有答案