1

URLから画像を取得するためにアセットライブラリを使用しています。私のコードは

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){     
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    UIImage *largeimage;

    if (iref) {
        largeimage = [UIImage imageWithCGImage:iref];
    }

    [self customeButtonCreated:self];

    NSData* thumbImageData = [[NSData alloc] initWithContentsOfFile:imagePath];
    btn1.frame = CGRectMake(x, y, WIDTH, HEIGHT);
    [btn1 setTag:tag];

    //[media_id addObject:[allKey objectAtIndex:0]];     
    [btnURl addObject:imagePath];

    UIImage *tempImage = [[UIImage alloc]initWithData:thumbImageData];
    [btn1 setImage:largeimage forState:UIControlStateNormal];
    btn1.imageView.contentMode = UIViewContentModeScaleAspectFit;           
    [tempImage release];

    [newView addSubview:btn1];
    [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [thumbImageData release];

    imgcount++;

    NSLog(@"Iamge count ---%d",imgcount);
    tag++;
};

ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){
    NSLog(@"Cannot get image - %@",[myerror localizedDescription]);
};


if ([seprateArr count] == 2) {
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"assets-library://asset/asset.JPG?id=%@&ext=%@",[seprateArr objectAtIndex:0],[seprateArr objectAtIndex:1]]];
    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock];
}

ios-4以下では正常に動作していますが、ios-5ではブロックに入らず、エラーを表示しています

画像を取得できません - グローバルにアクセスが拒否されました

これに対する解決策はありますか?

4

1 に答える 1

2

これは、ios 4.0 と ios 5 で動作します。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSString *photoName;
NSString *photoUrl;

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) 
    {
        if(result != NULL) 
        {
            NSArray *arrKeys = [[result valueForProperty:ALAssetPropertyURLs]allKeys];             
            if([[result valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto])
            {
                if([[UIDevice currentDevice] systemVersion]>=5.0))
                {
                    photoName = [[result defaultRepresentation]UTI];

                }
                else
                {
                    photoName = [[result defaultRepresentation]filename];

                }
                //Your code here
                photoUrl = [[result valueForProperty:ALAssetPropertyURLs]objectForKey:[arrKeys objectAtIndex:0]];

            }
        }
    };

    void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) 
    {
        if(group != nil) 
        {
            [group enumerateAssetsUsingBlock:assetEnumerator];
        }
    };
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:^(NSError *error){
        NSLog(@"%@",error);
    }];

    [pool release];
于 2012-06-13T09:55:21.873 に答える