嗨,我正在使用 Iphone 照片库。我使用 AssetFramework 从照片库中获取了所有照片。我在滚动视图中显示了这些照片并且完美显示并且图像计数假设为6。然后当我单击单个图像时,它将显示大图像。它也完成了。我的问题是“单击图像以将其显示为大时,计数为 12(双倍计数)。”
我使用下面的代码来获取图像:
- (void)createScrollView
{
@try
{
NSLog(@"in create scrollview");
//add views to scrolview
// UIImageView *backgroundImgView;
int x=5;
int y=7;
NSLog(@"assetsArray/count/createScrollview %d",assetsArray.count);
for (int i=0;i<[assetsArray count];i++)
{
UIView *userView=[[UIView alloc] initWithFrame:CGRectMake(x, y, 70, 80)];
userView.tag=i;
UIImageView *backgroundImgView=[[UIImageView alloc] initWithFrame:CGRectMake(1, 1, 70, 70)];
backgroundImgView.tag=1;
// [backgroundImgView setImageWithURL:[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF16BigEndianStringEncoding]] placeholderImage:[UIImage imageNamed:@"NoImage.png"]];
//-------------Getting Images from AssetsLibrary ----------
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
galleryObj=[[GalleryObject alloc]init];
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *assetsLibraryImage;
if (iref)
{
assetsLibraryImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
galleryObj.galleryImage=assetsLibraryImage;
}
else
{
assetsLibraryImage = [UIImage imageNamed:@"NoImage.png"];
}
//[set addObject:[NSString stringWithFormat:@"1"]];
[uniqueSet addObject:galleryObj];
NSLog(@"uniqueSet data is .....%@",uniqueSet); // Output (3,1,4,2,5) ... all objects
[imagesArray addObject:galleryObj];
NSLog(@"imagesArray/resultBlock count is %d array is %@....",imagesArray.count,imagesArray);
backgroundImgView.image=assetsLibraryImage;
};
ALAsset *al_asset = [assetsArray objectAtIndex:i];
//NSLog(@"al_asset is ......%@",al_asset);
al_assetUrl=al_asset.defaultRepresentation.url;
//NSLog(@"al_assetUrl is %@",al_assetUrl);
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"ALAssetsLibraryAccessFailureBlock");
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:al_assetUrl resultBlock:resultblock failureBlock:failureblock];
//-------------Getting Images from AssetsLibrary ----------
UIButton *userButton=[[UIButton alloc]initWithFrame:CGRectMake(1, 1, 70,70)];
[userButton addTarget:self action:@selector(userImageClicked:) forControlEvents:UIControlEventTouchUpInside];
userButton.tag=i;
[userView addSubview:backgroundImgView];
[userView addSubview:userButton];
[self.galleryScrollview addSubview:userView];
x+=79;
if ((i+1)%4==0)
{
//if added image is 4th image
y+=80;
x=5;
}
// [activity stopAnimating];
}
if (y+100>self.galleryScrollview.frame.size.height)
{
self.galleryScrollview.contentSize=CGSizeMake(320, y+100);
}
else
{
self.galleryScrollview.contentSize=CGSizeMake(320, self.galleryScrollview.frame.size.height+60);
}
}
@catch (NSException *exception)
{
NSLog(@"exception is %@",exception);
}
}
请注意,我在上述方法中创建了按钮和操作是 userImageClicked。当我单击 userImageClicked 按钮时,数组计数是双倍的。
我不知道为什么会这样。我尝试使用 containsObject 方法删除重复项。但没有用。
在上述方法中,我将该对象保存UIImage
并objectclass
分配给imagesArray
.
我也拿来NSMutableSet
储值了,但也没有用。
请任何人都可以建议解决我的问题。