gpt4 book ai didi

ios - UICollectionViewController 中的 ALAssets 不显示图像

转载 作者:行者123 更新时间:2023-11-29 04:11:53 25 4
gpt4 key购买 nike

我在在 collectionViewCell 的每个项目中显示设备库中的 ALAssets 列表时遇到问题。

我的图像在 Collection View 中不可见。我尝试了在亲戚帖子中找到的所有解决方案,但没有找到答案...为什么?

这是我的开发环境:

iPhone5,苹果6.0.2,Xcode 4.5,使用 ARC 和 Storyboard。

这是我的代码:

<强>1。首先,我在委托(delegate)中获取带有设备媒体的 MutableArray :

#AppDelegate.m

 -(void)loadPhotosFromDevice
{
//deviceMedia = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
// Within the group enumeration block, filter if necessary
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
// The end of the enumeration is signaled by asset == nil.
if (alAsset)
{
ALAssetRepresentation *representation = [alAsset defaultRepresentation];

[self addPhoto:representation andAsset:alAsset];
}
else
{
//NSLog(@"Done! Count = %d", deviceMedia.count);
//Do something awesome
}
}];
}
failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(@"No groups");
}];
}
}

<强>2。我使用“Media”对象类中的“AddPhoto”方法发送所有数据:

#AppDelegate.m

    -(void)addPhoto:(ALAssetRepresentation *)asset andAsset:alAsset
{
//NSLog(@"Adding photo!");

Media *media = [[Media alloc] init];
media.baseurl = @"";
media.url = [asset.url absoluteString];

Thumbnail *t = [[Thumbnail alloc] init];
t.rep = asset;
t.url = [asset.url absoluteString];

media.thumbnails = [[Thumbnail alloc] initWithRepresentation:asset];
media.name = asset.filename;
media.asset = alAsset;
media.isFromDevice = YES;
[getPhotos addObject:media];
NSLog(@"\nDevice Photo added array");
}

#Media.h

 import <AssetsLibrary/AssetsLibrary.h>

@interface Media : NSObject
{
//General
AudioInfo *audioInfo;
Info *info;
Header *header;
Status *requestStatus;
Exif *exif;
Thumbnail *thumbnails;
NSString *name;
NSString *uploaddt;
NSString *baseurl;
NSString *url;
NSString *createdt;
NSString *takendt;
NSString *recorddt;
ALAsset *asset;
bool isFromDevice;
}
//General

@property (nonatomic, retain) Thumbnail *thumbnails;
@property (nonatomic, retain) Status *requestStatus;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *uploaddt;
@property (nonatomic, retain) NSString *baseurl;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSString *createdt;
@property (nonatomic, retain) NSString *takendt;
@property (nonatomic, retain) NSString *recorddt;
@property (nonatomic, retain) ALAsset *asset;
@property (nonatomic, readwrite) bool isFromDevice;

@end

<强>3。然后我将它们显示在 CollectionViewController 中:

#PhotoDisplayCloudViewController

@implementation PhotoDisplayCloudViewController

- (void)viewDidLoad {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
tableau = appDelegate.getPhotos;
[super viewDidLoad];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return tableau.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//CELL
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
Media *object = [[Media alloc] init];
object = [tableau objectAtIndex:indexPath.row];

//IMAGE
NSString *url = [NSString stringWithFormat:@"%@%@", object.baseurl, object.thumbnails.url];
NSLog(@"%@ start loading", object.name);
UIImageView *image = [[UIImageView alloc] init];
if (object.isFromDevice)
{
image = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage :[object.asset.self thumbnail]]];
}
else
{
image = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: url]]]];
}
[image setFrame:CGRectMake(image.frame.origin.x, image.frame.origin.y, 72, 72)];

//LABEL
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(image.frame.origin.x+6, image.frame.origin.y+58, 60, 12)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = UIColor.blackColor;
label.contentMode = UIViewContentModeBottom;
label.font = [UIFont fontWithName:@"Times New Roman" size:9];
label.text = object.name;

//ADD VIEWS

[cell addSubview:image];
[cell addSubview:label];
NSLog(@"%@ end loading", object.name);

return cell;
}

- (void)didGrabImage:(UIImage *)image {
[collectionCell addSubview:image];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoDetailViewController *photoDetailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"photoDetail"];
photoDetailViewController.MaPhoto=[tableau objectAtIndex:indexPath.row];
[self.navigationController pushViewController:photoDetailViewController animated:YES];

}
@end

请清楚地回答我^^

提前谢谢!

最佳答案

我找到了问题的答案:

感谢“adaydesign的博客”,我使用这个方法将我的资源加载到UIImageView中:

效果很好!

-(void)loadPhotoFromURL:(NSURL*)imgURL thumbnail:(BOOL)useThumbnail showIn:(UIImageView*)imView{

if (imgURL!=nil) {

ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *myasset){

CGImageRef iref;

if (useThumbnail) {

iref = [myasset thumbnail];

}else {

ALAssetRepresentation *rep = [myasset defaultRepresentation];

iref = [rep fullScreenImage];

}

if (iref) {

UIImage *resPhoto = [UIImage imageWithCGImage:iref];

//NSLog(@"photo size:%f x %f",resPhoto.size.width,resPhoto.size.height);

imView.image = resPhoto;

}

};//end result block


ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error){

NSLog(@"error....");

};//end failureBlock


ALAssetsLibrary *assetLib = [[[ALAssetsLibrary alloc] init] autorelease];

[assetLib assetForURL:imgURL

resultBlock:resultBlock

failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock];

}//end if

}//end function

关于ios - UICollectionViewController 中的 ALAssets 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14282346/

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