我正在使用以下代码从资产库加载图像。它不起作用,如果我单击 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];
}