调用委托后应用崩溃

时间:2018-11-05 13:24:15

标签: ios delegates uiimage nsoperationqueue nsoperation

我有一个NSOperation类,它从PHASSET生成大小图像。就像这样:

#import "MultipleImageOperation.h"

@implementation MultipleImageOperation

@synthesize asset = _asset;
@synthesize tag = _tag;
@synthesize isLogo = _isLogo;
@synthesize delegate = _delegate;

-(id)initWithAsset:(PHAsset*) asset andTag:(NSUInteger)theTag andDelegate:(id<MultipleImageOperationDelegate>)theDelegate {
    if(self = [super init]) {
        _asset = asset;
        _tag = theTag;
        _delegate = theDelegate;
    }
    return self;
}

- (void)main {
    @autoreleasepool {
        __block UIImage *smallImage, *largeImage;
        if (self.isCancelled == NO){
            PHImageRequestOptions *requestOptions =  [[PHImageRequestOptions alloc] init];
            requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
            requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
            requestOptions.synchronous = true;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            PHImageManager *manager = [PHImageManager defaultManager];

            [manager requestImageForAsset:_asset
                     targetSize:!_isLogo? LARGE_IMAGE_SIZE : COMPANY_LOGO_LARGE_SIZE
                     contentMode:PHImageContentModeAspectFit
                     options:requestOptions
                     resultHandler:^void(UIImage *image, NSDictionary *info) {

                         NSData *imageData = UIImagePNGRepresentation(image);
                         NSString *filename = [NSString stringWithFormat:@"/img_%f.png", [[Utils getCurrentDate] timeIntervalSince1970]];
                         NSString *tempPath = [documentsDirectory stringByAppendingString:filename];
                         [imageData writeToFile:tempPath atomically:YES];
                         largeImage = image;
            }];
            [manager requestImageForAsset:_asset
                     targetSize:!_isLogo? SMALL_IMAGE_SIZE : COMPANY_LOGO_SMALL_SIZE
                     contentMode:PHImageContentModeAspectFit
                     options:requestOptions
                     resultHandler:^void(UIImage *image, NSDictionary *info) {

                         smallImage = image;
            }];
            if ([self.delegate respondsToSelector:@selector(multipleImageProcessingOperationCompletedForTag:withSmallImage:andLargeImage:)]) {
                    [self.delegate multipleImageProcessingOperationCompletedForTag:_tag withSmallImage:smallImage andLargeImage:largeImage];
            }

        }
    }
}


@end

代理方法如下:

- (void) multipleImageProcessingOperationCompletedForTag:(NSUInteger)tag withSmallImage:(UIImage *)smallImage andLargeImage:(UIImage*)largeImage {
    Media *tempMedia;
    tempMedia = [self.imagesList objectAtIndex:tag];
    tempMedia.smallImage = smallImage;
    tempMedia.mediaContent = largeImage;
    tempMedia.mediaContentSize = [NSNumber numberWithDouble:([Utils convertToNSDataFromUIImage:largeImage].length/BYTES_IN_GB)];

    [self updateImagesList:tempMedia];
    photoOperations--;
    if ( photoOperations == 0 ) {
        if ([self.delegate respondsToSelector:@selector(updateDisplay)]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.delegate updateDisplay];
            });
        }
    }
}
  

018-11-05 16:20:09.633 [722:137251] ***-[UIImage保留]:   消息发送到已释放实例0x198dbe20

小图永远是零!我被困住了!

1 个答案:

答案 0 :(得分:0)

我对您的图像生成库一无所知,但是看起来用来创建小图像的方法是异步的。这意味着,当您调用委托方法时,小型图像方法可能尚未完成其任务。我会的:

[manager requestImageForAsset:_asset
    targetSize:!_isLogo? SMALL_IMAGE_SIZE : COMPANY_LOGO_SMALL_SIZE
    contentMode:PHImageContentModeAspectFit
    options:requestOptions
    resultHandler:^void(UIImage *image, NSDictionary *info) {
        smallImage = image;
        if ([self.delegate respondsToSelector:@selector(multipleImageProcessingOperationCompletedForTag:withSmallImage:andLargeImage:)]) 
        {
            [self.delegate multipleImageProcessingOperationCompletedForTag:_tag withSmallImage:smallImage andLargeImage:largeImage];
        }
}];

但是您还必须确保完成大图像方法(如果它也是异步的)。