- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含与 assetslibrary 中的图像相对应的 imageurls 的数组,我需要在执行某个任务之前获取所有这些图像。这样做的最佳方法是什么?我应该使用 NSNotificationCenter
还是最好使用 block ,如果是这样,有什么例子吗?
这是我的代码:
- (IBAction)buttonClicked:(id)sender {
NSMutableArray* images = [NSMutableArray array];
//Need to loop through the takenImagesURLArray
for (NSURL *imageURL in takenImagesURLArray) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:imageURL
resultBlock:^(ALAsset *asset) {
if (asset) {
NSLog(@"HAS ASSET: %@", asset);
UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
[images addObject:image];
} else {
NSLog(@"Something went wrong");
}
}
failureBlock:^(NSError *error) {
NSLog(@"Something went wrong, %@", error);
}];
}
//This will of course be called before images is ready
[self doCertainTaskWith: images];
}
最佳答案
您可以为此使用 Grand Central Dispatch。
但首先,移动 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
在 for 循环之外,因为它可以被重用。
现在是真正的代码。它看起来像这样:
dispatch_group_t dgroup = dispatch_group_create(); //0
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
for (NSURL *imageURL in urls) {
dispatch_group_enter(dgroup); // 1
[library assetForURL:imageURL resultBlock:^(ALAsset *asset) {
if (asset) {
NSLog(@"HAS ASSET: %@", asset);
UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
[images addObject:image];
dispatch_group_leave(dgroup); //2
} else {
NSLog(@"Something went wrong");
dispatch_group_leave(dgroup);
}
} failureBlock:^(NSError *error) {
NSLog(@"Something went wrong, %@", error);
dispatch_group_leave(dgroup);
}];
}
dispatch_group_notify(dgroup, dispatch_get_main_queue(), ^{ //3
NSLog(@"Do something with %d images", [images count]);
});
retain
支持一个对象,在这种情况下,我们的组。 release
在一个物体上。 关于ios - ALAssetsLibrary,等到assetForURL 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22475947/
我只想使用 Swift 从 ios 8 中的 ALAsset 获取图像资源。 我使用 SNImagePicker 进行多张图片选取。 对于 objective-c,我使用这个: for (in
我正在尝试使用 AssetForURL 方法,但它返回 nil。 这是我正在使用的代码: -(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)
我想等待此代码执行后再继续,但由于这些 block 是异步调用的,我不知道该怎么做??? NSURL *asseturl; NSMutableArray *tmpListAsset = [[NSMut
我正在尝试将此代码从 obj-c 转换为 swift 2.1: [[ALAssetsLibrary new] assetForURL:info[UIImagePickerControllerRefer
在 MonoTouch 中: 我正在将相机中的图像放入 PhotoAlbum 中: ALAssetsLibrary library = new ALAssetsLibrary(); library.W
我正在尝试获取照片库中的图像,为此我正在使用 assetForURL,但我一直收到错误消息“无法使用类型为‘( NSURL, resultBlock: (ALAsset!) -> Void, (NSE
我正在尝试从用户选择的图像中读取 EXIF 数据。我为此使用 ALAssetLibrary。到目前为止,我已经设法获得了 assetForURL:resultBlock:failureBlock: 方
我正在尝试创建一个方法,该方法将为给定的 Assets url 返回一个 ALAsset。 (我需要稍后上传 Assets ,并希望在结果块之外使用结果进行上传。) + (ALAsset*) asse
我正在使用 UIImagePickerController 选择图像,但我还需要图像的名称和其他元数据信息。我可以在 UIImagePickerController 提供的 referenceURL
此代码在 iOS 7 中运行良好,但在 iOS 8.1 中,位于“我的照片流”相册中的所有 Assets 在结果 block 中都为零。 (不调用 failureBlock。)普通相册和共享相册工作正
我是一名优秀的程序员,十分优秀!