如何将设备中的图像复制到应用程序的沙箱中?

时间:2014-08-25 08:23:25

标签: ios objective-c

我需要将设备中的图像复制到应用程序的沙箱中!在Library / Cache文件夹中说!我该怎么做?我在互联网上搜索我没有找到解决方案。请帮忙!

修改

#pragma mark - ZYQAssetPickerController Delegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
    ALAssetsLibrary *lib=[ALAssetsLibrary new];
    for (int i=0; i<assets.count; i++) {
            ALAsset *asset=assets[i];
            FileOP *fileMgr=[[FileOP alloc]init];
            NSString *baseDir=[fileMgr GetDocumentDirectory];
            //STORING FILE INTO LOCAL
            [lib assetForURL:asset.defaultRepresentation.url
                 resultBlock:^(ALAsset *asset){
                     ALAssetRepresentation *repr = [asset defaultRepresentation];
                     CGImageRef cgImg = [repr fullResolutionImage];
                     NSString *fname = repr.filename;
                     UIImage *img = [UIImage imageWithCGImage:cgImg];
                     NSData *data = UIImagePNGRepresentation(img);
                     [data writeToFile:[baseDir stringByAppendingPathComponent:fname]
                            atomically:YES];
                     //FOR LOCAL URL OF THE IMAGE
                     NSString *imageURL = [baseDir stringByAppendingPathComponent:fname];

                     NSLog(@"%@ URL OF IMAGE ",imageURL);
                     [[ImageArray sharedImageArray]setImageUrl:imageURL atIndex:i];
                     //NSLog(@"%@ is the shared array",[[ImageArray sharedImageArray] getImageUrlAtIndex:i];
                           }

                failureBlock:^(NSError *error){
                }];
        }
        NSLog(@"COPIED %lu FILE INTO LOCAL MEMORY",(unsigned long)assets.count);

    //NEED TO STORE THE PATH OF THE SELECTED IMAGE FILES INTO SOME ARRAY HERE



}

1 个答案:

答案 0 :(得分:0)

你可以这样做:

在.h中,实现UIImagePickerControllerDelegate委托:

@interface yourClass : NSObject <UIImagePickerControllerDelegate>

在.m中,创建一个单击按钮时触发的方法。它将打开您的照片库并将拾取的图像保存到缓存文件夹中:

- (IBAction)pickFromAlbum:(id)sender
{
    UIImagePickerController *pickerController = [UIImagePickerController new];
    pickerController.delegate = self;
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:pickerController animated:YES completion:nil];
}

然后实现委托方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Get image into a variable
    UIImage *takenImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    // Get the cache folder
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDir = [dirPaths objectAtIndex:0];

    // Save image into cache folder
    NSData *data;
    data = [NSData dataWithData:UIImageJPEGRepresentation(takenImage, 1.0f)]; // 1.0f = quality 100%
    [data writeToFile:[cacheDir stringByAppendingPathComponent:@"fileName.jpeg"] atomically:YES];

    // Hide the picker
    [picker dismissViewControllerAnimated:YES completion:nil];
}