我把这个问题作为我之前发表的主题的续篇。
我正在ios应用程序中捕获图像和视频,并将其保存到相机胶卷中(客户要求)。但是,为了查看捕获的图像/视频,我将核心数据中的assetURL保存为字符串。
我的保存代码如下:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *cameraMedia = [info objectForKey:UIImagePickerControllerOriginalImage ];
NSDictionary* metaData = [info objectForKey:UIImagePickerControllerMediaMetadata];
NSString* mediaType = [info objectForKey: UIImagePickerControllerMediaType];
NSURL* mediaUrl = (NSURL*)[info valueForKey:UIImagePickerControllerMediaURL];
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
//For now we are saving image capture time from here
//In future we may need to load the time from image metaData
NSDate *captureTime = [[NSDate alloc] init];
//completion blocks
ALAssetsLibraryWriteImageCompletionBlock imageCompletionBlock = ^(NSURL* imgURL, NSError* error){
if (error == nil) {
NSLog(@"Asset URL %@", imgURL);
[self saveEvidence:imgURL mediaType:mediaType withDate:captureTime];
} else {
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:@"Failed to save image to device!"
message:[error localizedDescription]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:Nil];
[alertView show];
}
};
ALAssetsLibraryWriteVideoCompletionBlock videoCompletionBlock = ^(NSURL* videoURL, NSError* error){
if(error == nil)
{
NSLog(@"Asset URL %@", videoURL);
[self saveEvidence:videoURL mediaType:mediaType withDate:captureTime];
}else
{
UIAlertView *alertView =
[[UIAlertView alloc] initWithTitle:@"Failed to save video to device!"
message:[error localizedDescription]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:Nil];
[alertView show];
}
};
if(CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){
[al writeImageToSavedPhotosAlbum:[cameraMedia CGImage] metadata:metaData completionBlock: imageCompletionBlock];
}else if(CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie , 0) == kCFCompareEqualTo){
[al writeVideoAtPathToSavedPhotosAlbum:mediaUrl completionBlock:videoCompletionBlock];
}
[self dismissViewControllerAnimated:YES completion:nil];
}在saveEvidence中,我将URL保存到我的核心数据实体。现在我加载我的镜像,如下所示:
-(void)loadThumbNail:(NSString*) mediaURL{
NSLog(@"image URL String: %@", mediaURL);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset)
{
NSLog(@"in result block");
//self.thumbNailImage = [UIImage imageWithCGImage:[asset thumbnail]];
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
self.thumbNailImage = [UIImage imageWithCGImage:iref];
}
};
//
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *error)
{
NSLog(@"Cant load image - %@",[error localizedDescription]);
};
if(mediaURL && [mediaURL length])
{
self.thumbNailImage = nil;
NSURL *asseturl = [NSURL URLWithString:mediaURL];
NSLog(@"image URL: %@", asseturl);
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:asseturl
resultBlock:resultblock
failureBlock:failureblock];
}
}最初,我尝试使用: self.thumbNailImage = [UIImage imageWithCGImage:asset thumbnail ]加载缩略图,但图像仍然为空,也就是没有加载。然后我尝试加载fullResolutionImage。尽管如此,图像仍然是空的。
有什么解决方案吗?
发布于 2013-12-02 01:02:41
尝试像这样获取资产
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
if (alAsset)
{
ALAssetRepresentation *representation =[alAsset defaultRepresentation];
NSURL *url = [representation url];
if([url isEqualToString:YOUR_SAVED_URL])
{
NSString *assetType=[alAsset valueForProperty:ALAssetPropertyType];
UIImage *thumbNailImage=[UIImage imageWithCGImage:alAsset.thumbnail];
}
}
}];
if(group==nil)
{
// do what ever if group getting null
}
} failureBlock: ^(NSError *error)
{
// may be photo privacy setting disabled
}
];https://stackoverflow.com/questions/20314525
复制相似问题