Iphone ALAssetLibrary和iOS 5.0
我编写了从资产库中获取数据的简单方法,该方法在iOS4.3上运行良好,但在iOS5中获取图像时会出现延迟。我应该如何加快iOS 5上的抓取过程Iphone ALAssetLibrary和iOS 5.0,iphone,ios4,ios5,Iphone,Ios4,Ios5,我编写了从资产库中获取数据的简单方法,该方法在iOS4.3上运行良好,但在iOS5中获取图像时会出现延迟。我应该如何加快iOS 5上的抓取过程 -(void)setImages { int count =0; int photoNumber = [[templateDictionary objectForKey:@"ElementsOnPage"] intValue]; for (int i=currentCount; count<photoNumber; i++
-(void)setImages
{
int count =0;
int photoNumber = [[templateDictionary objectForKey:@"ElementsOnPage"] intValue];
for (int i=currentCount; count<photoNumber; i++) {
[self data:count+1 count:i];
count++;
}
}
-(void)data:(int)photoNumber count:(int)currentCount
{
NSURL *url;
UIImageView *firstImageView = [[UIImageView alloc]init];
CGFloat x,y,wid,h;
float ang;
if (currentCount>=[ImageURLArray count]) {
[firstImageView release];
return;
}
else
{
url = [NSURL URLWithString:[ImageURLArray objectAtIndex:currentCount]];
switch (photoNumber) {
case 1:
{
x = [[templateDictionary objectForKey:@"FirstElement_X"]floatValue];
y=[[templateDictionary objectForKey:@"FirstElement_Y"]floatValue];
wid = [[templateDictionary objectForKey:@"FirstElement_Width"]floatValue];
h=[[templateDictionary objectForKey:@"FirstElement_Height"]floatValue];
ang =[[templateDictionary objectForKey:@"FirstElement_Angle"]floatValue];
firstImageView.tag = 1+10;
//FirstImage
}
break;
case 2:
{
x = [[templateDictionary objectForKey:@"SecondElement_X"]floatValue];
y=[[templateDictionary objectForKey:@"SecondElement_Y"]floatValue];
wid = [[templateDictionary objectForKey:@"SecondElement_Width"]floatValue];
h=[[templateDictionary objectForKey:@"SecondElement_Height"]floatValue];
ang =[[templateDictionary objectForKey:@"SecondElement_Angle"]floatValue];
firstImageView.tag=2+10;
//SecondImage
}
break;
case 3:
{
x = [[templateDictionary objectForKey:@"ThirdElement_X"]floatValue];
y=[[templateDictionary objectForKey:@"ThirdElement_Y"]floatValue];
wid = [[templateDictionary objectForKey:@"ThirdElement_Width"]floatValue];
h=[[templateDictionary objectForKey:@"ThirdElement_Height"]floatValue];
ang =[[templateDictionary objectForKey:@"ThirdElement_Angle"]floatValue];
firstImageView.tag = 3+10;
//ThirdImage
}
break;
default:
break;
}
[firstImageView setFrame:CGRectMake(x, y, wid, h)];
dispatch_async(dispatch_get_main_queue(), ^
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library assetForURL:url
resultBlock:^(ALAsset* asset)
{
UIImage* img = [UIImage imageWithCGImage:[asset.defaultRepresentation fullResolutionImage]];
[firstImageView setImage:img];
}
failureBlock:^(NSError* error)
{
NSLog(@"error requesting asset");
}
];
[library release];
// Group enumerator Block
[pool release];
});
if ([[contentType objectAtIndex:currentCount]isEqualToString:@"1"]) {
UIButton *videoImage = [[UIButton alloc]initWithFrame:CGRectMake((firstImageView.frame.size.width/2)-25,(firstImageView.frame.size.height/2)-25,50,50)];
videoImage.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
[videoImage setBackgroundImage:[UIImage imageNamed:@"videothumb.png"] forState:UIControlStateNormal];
[videoImage addTarget:self action:@selector(PlayMusicOnClickofButton:) forControlEvents:UIControlEventTouchUpInside];
[firstImageView addSubview:videoImage];
videoImage.tag = currentCount+1000;
[videoImage release];
}
}
firstImageView.transform = CGAffineTransformMakeRotation(ang*(3.14/180));
firstImageView.userInteractionEnabled = YES;
[coverImageView addSubview:firstImageView];
[coverImageView setImage:[UIImage imageNamed:innerBackground]];
[firstImageView release];
-(无效)设置图像
{
整数计数=0;
int photoNumber=[[templateDictionary objectForKey:@“ElementsOnPage”]intValue];
对于(int i=currentCount;count=[ImageURLArray count]){
[firstImageView发布];
返回;
}
其他的
{
url=[NSURL URLWithString:[ImageURLArray objectAtIndex:currentCount]];
开关(照片编号){
案例1:
{
x=[[templateDictionary objectForKey:@“FirstElement_x”]floatValue];
y=[[templateDictionary objectForKey:@“FirstElement_y”]floatValue];
wid=[[templateDictionary objectForKey:@“FirstElement_Width”]floatValue];
h=[[templateDictionary objectForKey:@“FirstElement_Height”]floatValue];
ang=[[templateDictionary objectForKey:@“FirstElement_Angle”]floatValue];
firstImageView.tag=1+10;
//第一映像
}
打破
案例2:
{
x=[[templateDictionary objectForKey:@“SecondElement_x”]floatValue];
y=[[templateDictionary objectForKey:@“SecondElement_y”]floatValue];
wid=[[templateDictionary objectForKey:@“SecondElement_Width”]floatValue];
h=[[templateDictionary objectForKey:@“SecondElement_Height”]floatValue];
ang=[[templateDictionary objectForKey:@“SecondElement_Angle”]floatValue];
firstImageView.tag=2+10;
//第二映像
}
打破
案例3:
{
x=[[templateDictionary objectForKey:@“ThirdElement_x”]floatValue];
y=[[templateDictionary objectForKey:@“ThirdElement_y”]floatValue];
wid=[[templateDictionary objectForKey:@“ThirdElement_Width”]floatValue];
h=[[templateDictionary objectForKey:@“ThirdElement_Height”]floatValue];
ang=[[templateDictionary objectForKey:@“第三个数据元素角度”]floatValue];
firstImageView.tag=3+10;
//三十岁
}
打破
违约:
打破
}
[firstImageView设置帧:CGRectMake(x,y,wid,h)];
dispatch\u async(dispatch\u get\u main\u queue()^
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
AlassetLibrary*library=[[AlassetLibrary alloc]init];
//使用AlassetGroupSavedPhotos仅枚举照片和视频组。
[library assetForURL:url
结果块:^(ALAsset*资产)
{
UIImage*img=[UIImage imageWithCGImage:[asset.defaultRepresentation fullResolutionImage]];
[firstImageView设置图像:img];
}
故障块:^(N错误*错误)
{
NSLog(@“请求资产时出错”);
}
];
[图书馆发行];
//组枚举器块
[池释放];
});
如果([[contentType objectAtIndex:currentCount]isEqualToString:@“1”]){
UIButton*videoImage=[[UIButton alloc]initWithFrame:CGRectMake((firstImageView.frame.size.width/2)-25,(firstImageView.frame.size.height/2)-25,50,50)];
videoImage.transform=CGAffineTransformMakeRotation(ang*(3.14/180));
[videoImage setBackgroundImage:[UIImage ImageName:@“videothumb.png”]用于状态:UIControlStateNormal];
[videoImage addTarget:self action:@selector(播放音乐单击按钮:)for ControlEvents:UIControlEventTouchUpInside];
[firstImageView添加子视图:视频图像];
videoImage.tag=当前计数+1000;
[影像发布];
}
}
firstImageView.transform=CGAffineTransformMakeRotation(ang*(3.14/180));
firstImageView.userInteractionEnabled=是;
[coverImageView添加子视图:firstImageView];
[coverImageView setImage:[UIImage ImageName:innerBackground]];
[firstImageView发布];
}我已经向苹果公司报告了一个关于iOS5中assetForUrl性能下降的bug(在开发论坛上苹果公司的一名员工提出请求后) 背景:assetLibrary经过重构,现在基于CoreData,在每次assetForUrl调用时,SDK实际上会打开一个新的SQLite连接(BAH…),从而导致显著的性能损失
临时解决方案:在我的应用程序中,我需要使用assetForUrl加载200张图片。在IOS4中需要100毫秒,在iOS5中大约需要5秒以上。我发现枚举整个库(大约1500张图片)并将其缓存在URL-->资产字典中大约需要3秒钟。我现在正在使用这种技术。如果您持有过时的资产并对库进行更改,请小心它们。+1表示保留。你知道答案了吗?不,我没有解决延迟的办法。内存没有问题吗?此解决方案似乎不可扩展。