从ios assetlibrary加载图片

时间:2013-08-09 18:35:36

标签: iphone ios objective-c

我正在使用以下代码从assetlibrary加载图片。它不起作用,如果我点击displayImage uiimageviewer,它会显示无效的尝试访问其拥有的ALAssetsLibrary的生命周期。

标头文件

#import <UIKit/UIKit.h>
#import "BFCropInterface.h"
#import "GRKImage.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface BFViewController : UIViewController{
    GRKImage *gImg;
}

@property (nonatomic, strong) IBOutlet UIImageView *displayImage;
@property (nonatomic, strong) UIImage *originalImage;
@property (nonatomic, strong) BFCropInterface *cropper;
@property (nonatomic, retain) GRKImage *gImg;
@property (strong, atomic) ALAssetsLibrary* library;

- (IBAction)cropPressed:(id)sender;
- (IBAction)originalPressed:(id)sender;
- (IBAction)savePressed:(id)sender;



@end

主文件:viewDidload

if ( [[self.gImg.URL absoluteString] hasPrefix:@"assets-library://"] ){


    typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
    typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){

        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        float scale=[rep scale];
        ALAssetOrientation orientation=[rep orientation];
        if (iref){

            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *originalImage = [UIImage imageWithCGImage:iref scale:scale orientation:(UIImageOrientation)orientation];

                self.displayImage.contentMode = UIViewContentModeScaleAspectFit;

                [self.displayImage setImage:originalImage];

            });


        }
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){

        //failed to get image.
    };

    self.library = [[ALAssetsLibrary alloc] init];
    [self.library assetForURL:self.gImg.URL resultBlock:resultblock failureBlock:failureblock];
     }

1 个答案:

答案 0 :(得分:0)

您目前正在尝试在主线程上使用ALAssetRepresentation rep,此时assetslibrary将被释放。尝试将assetslibrary存储为类中的属性,或者在库仍然有效时将scaleorientation存储在块之外。


如果您只是显示它,也不需要使用fullResolutionImage。请改用fullScreenImage