我正在使用以下代码从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];
}
答案 0 :(得分:0)
您目前正在尝试在主线程上使用ALAssetRepresentation
rep
,此时assetslibrary
将被释放。尝试将assetslibrary
存储为类中的属性,或者在库仍然有效时将scale
和orientation
存储在块之外。
如果您只是显示它,也不需要使用fullResolutionImage
。请改用fullScreenImage
。