[英]Loading image from ios assetlibrary
我正在使用以下代碼從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];
}
您當前正在嘗試在主線程上使用ALAssetRepresentation
rep
,此時將釋放assetslibrary
。 嘗試將assetslibrary
作為屬性存儲在類中,或者在庫仍然有效時將scale
和orientation
assetslibrary
塊。
如果只顯示它,則也不需要使用fullResolutionImage
。 請改用fullScreenImage
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.