gpt4 book ai didi

iphone - NSBlockOperation 或带有 ALAsset Block 的 NSOperation 使用 ALAsset URL 显示照片库图像

转载 作者:行者123 更新时间:2023-12-03 20:21:59 25 4
gpt4 key购买 nike

我正在就我的问题提出这个问题Display photolibrary images in an effectual way iPhone Highly efficient UITableView "cellForRowIndexPath" method to bind the PhotoLibrary images

所以我想请求在不阅读以下详细信息的情况下不要与此重复答案:)

让我们来讨论一下这个问题

我对上述问题进行了详细研究,并从 here 找到了有关操作队列的文档.

因此,我创建了一个示例应用程序,通过 ALAsset block 使用操作队列来显示七个照片库图像。

以下是示例应用程序详细信息。

第 1 步:

在 NSOperationalQueueViewController viewDidLoad 方法中,我检索了所有照片库 ALAsset URLs到名为 urlArray 的数组中。

第 2 步:

将所有 URL 添加到 urlArray 后,assetGroupEnumerator 中的 if(group != nil) 条件将为 false,因此我创建了一个 NSOperationQueue,然后通过 for 循环创建了七个 UIImageView,并使用相应的图像创建了我的 NSOperation 子类对象查看每一个的和URL,并将它们添加NSOperationQueue中。

查看我的 NSOperation 子类 here .

查看我的实现 (VierwController) 类 here .

让我们来讨论这个问题。

它无法一致地显示所有七个图像。部分图像丢失。丢失的顺序多次更改(一次不显示第六和第七,另一次不显示仅第二和第三)。控制台日志显示找不到照片图片编号。但是,URL 已正确记录。

可以看到日志详细信息here .

我的类(class)有错误吗?

另外,当我浏览上面提到的 operational queue 时文档,我已阅读有关 NSBlockOperation 的内容。在处理 ALAsset block 时,我是否需要实现 NSBlockOperation 而不是 NSOperation

NSBlockOperation 描述如下

A class you use as-is to execute one or more block objects concurrently. Because it can execute more than one block, a block operation object operates using a group semantic; only when all of the associated blocks have finished executing is the operation itself considered finished.

如何在我的示例应用程序中使用 ALAsset block 实现 NSBlockOperation

我已经解决了 Stack Overflow 问题 Learning NSBlockOperation 。但是,我不知道如何使用 ALAsset block 实现 NSBlockOperation!!

最佳答案

这是关于“如何使用 ALAsset Library 访问 iPhonePhoto Library 中的所有图像并像 iPhoneSimulator 一样在 UIScrollView 上显示它们”的教程。首先添加AssetsLibrary.framework到您的项目。

然后在你的viewController.h中文件导入#import <AssetsLibrary/AssetsLibrary.h>头文件。

这是你的viewController.h文件

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "AppDelegate.h"

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
ALAssetsLibrary *assetsLibrary;
NSMutableArray *groups;
ALAssetsGroup *assetsGroup;

// I will show all images on `UIScrollView`
UIScrollView *myScrollView;

UIActivityIndicatorView *activityIndicator;

NSMutableArray *assetsArray;
// Will handle thumbnail of images
NSMutableArray *imageThumbnailArray;
// Will handle original images
NSMutableArray *imageOriginalArray;

UIButton *buttonImage;
}

-(void)displayImages;
-(void)loadScrollView;

@end

这是你的viewController.m文件 - viewWillAppear:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

assetsArray = [[NSMutableArray alloc]init];
imageThumbnailArray = [[NSMutableArray alloc]init];
imageOriginalArray = [[NSMutableArray alloc]init];

myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 416.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 416.0);
myScrollView.backgroundColor = [UIColor whiteColor];

activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = myScrollView.center;
[myScrollView addSubview:activityIndicator];
[self.view addSubview:myScrollView];

[activityIndicator startAnimating];

}

viewDidAppear:

-(void)viewDidAppear:(BOOL)animated
{
if (!assetsLibrary) {
assetsLibrary = [[ALAssetsLibrary alloc] init];
}
if (!groups) {
groups = [[NSMutableArray alloc] init];
}
else {
[groups removeAllObjects];
}

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
//NSLog(@"group %@",group);
if (group) {
[groups addObject:group];
//NSLog(@"groups %@",groups);
} else {
//Call display Images method here.
[self displayImages];
}
};
ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error) {
NSString *errorMessage = nil;
switch ([error code]) {
case ALAssetsLibraryAccessUserDeniedError:
case ALAssetsLibraryAccessGloballyDeniedError:
errorMessage = @"The user has declined access to it.";
break;
default:
errorMessage = @"Reason unknown.";
break;
}
};
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:listGroupBlock failureBlock:failureBlock];

}

这是displayImages:方法体

-(void)displayImages
{
// NSLog(@"groups %d",[groups count]);
for (int i = 0 ; i< [groups count]; i++) {
assetsGroup = [groups objectAtIndex:i];
if (!assetsArray) {
assetsArray = [[NSMutableArray alloc] init];
}
else {
[assetsArray removeAllObjects];
}

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

if (result) {
[assetsArray addObject:result];
}
};
ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
[assetsGroup setAssetsFilter:onlyPhotosFilter];
[assetsGroup enumerateAssetsUsingBlock:assetsEnumerationBlock];

}

//Seprate the thumbnail and original images
for(int i=0;i<[assetsArray count]; i++)
{
ALAsset *asset = [assetsArray objectAtIndex:i];
CGImageRef thumbnailImageRef = [asset thumbnail];
UIImage *thumbnail = [UIImage imageWithCGImage:thumbnailImageRef];
[imageThumbnailArray addObject:thumbnail];

ALAssetRepresentation *representation = [asset defaultRepresentation];
CGImageRef originalImage = [representation fullResolutionImage];
UIImage *original = [UIImage imageWithCGImage:originalImage];
[imageOriginalArray addObject:original];
}

[self loadScrollView];
}

现在你有两个array一是 imageThumbnailArray另一个是 imageOriginalArray 。使用imageThumbnailArray用于显示 UIScrollView你的滚动不会很慢......并使用 imageOriginalArray以获得图像的放大预览。

'loadScrollView:'方法,这是如何在 UIScrollView 上显示图像就像iPhone模拟器

#pragma mark - LoadImages on UIScrollView
-(void)loadScrollView
{
float horizontal = 8.0;
float vertical = 8.0;

for(int i=0; i<[imageThumbnailArray count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}

buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[ buttonImage setImage:[imageThumbnailArray objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;

}

[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}

在这里您可以找到哪个图像 button已被点击 -

#pragma mark - Button Pressed method
-(void)buttonImagePressed:(id)sender
{
NSLog(@"you have pressed : %d button",[sender tag]);
}

希望本教程能够帮助您和许多搜索相同内容的用户。谢谢!

关于iphone - NSBlockOperation 或带有 ALAsset Block 的 NSOperation 使用 ALAsset URL 显示照片库图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com