0

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

1 回答 1

0

您当前正在尝试ALAssetRepresentation rep在主线程上使用,此时assetslibrary将已发布。尝试将 存储assetslibrary为类中的属性,或者在库仍然有效时获取块之外的scale和。orientation


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

于 2013-08-09T18:44:18.040 回答