完成分配后,UIImage 在函数结束时被释放

UIImage gets released at the end the function after assignment has been made

我有一个 ViewController 将自定义 UIView 作为其成员,如下所示:@属性(非原子,强)IBOutlet ProfileUIView * profileUIView;
在我的 ViewController 我从 imagePickerController:didFinishPickingMediaWithInfo: 取回一个 UIImage,然后我将获得的 UIImage 分配给 ProfileUIView 中的本地 UIImage 变量。我看到 UIImage 变量被分配,但是,在 set 函数结束后 UIImage 变量得到 nil 并且我以某种方式丢失了图像。

这是 imagePickerController:didFinishPickingMediaWithInfo 的片段:在我的 ViewController:

NSString *mediaType = info[UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:NO completion:nil];

if ([mediaType isEqualToString:(NSString *) kUTTypeImage]) {

    [self.profileUIView setPicture:info[UIImagePickerControllerOriginalImage]];

    if (_newMedia)
        UIImageWriteToSavedPhotosAlbum(self.profileImage, self, @selector(image:finishedSavingWithError:contextInfo:), nil);
}

这里是 setPicture:(UIImage *)image: 在我的 UIView:

中的实现
self.profilePic = image;    
[self.pictureButton setBackgroundImage:image forState:UIControlStateNormal];


我该如何解决?
谢谢!

ViewController.h

这里我们确保导入正确的框架,持有 UIImageProfileUIView 实例。我们还声明了一个选择图像的方法,当在 Interface Builder 中连接时,可以从例如相应视图上的按钮调用该图像:

#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "ProfileUIView.h"


@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, strong) UIImage *profilePic;
@property (strong, nonatomic) IBOutlet ProfileUIView *profileUIView;
- (IBAction)pickImage:(id)sender;

@end

ViewController.m中,我们实现了选择图像的方法和用户选择图像后调用的委托方法:

- (IBAction)pickImage:(id)sender {
    UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];
    pickerC.delegate = self;
    [self presentViewController:pickerC animated:YES completion:nil];
}

#pragma mark - UIImagePicker delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:NO completion:nil];
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *) kUTTypeImage]) {
        self.profilePic = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self.profileUIView setPicture:self.profilePic];
    }
}

ProfileUIView.h

这里声明头像UIImage,显示图片的UIButton,以及设置图片的方法:

#import <UIKit/UIKit.h>

@interface ProfileUIView : UIView

@property (nonatomic, retain) UIImage *profilePic;
@property (strong, nonatomic) IBOutlet UIButton *pictureButton;

- (void) setPicture:(UIImage *)image;

@end

ProfileUIView.m

这里我们确保从NIB唤醒时UIButton在位,并实现set picture方法将图片设置为UIButton:

的背景
#import "ProfileUIView.h"

@implementation ProfileUIView

-(void)awakeFromNib {
    self.pictureButton = [[UIButton alloc] initWithFrame:self.bounds];
    [self addSubview:self.pictureButton];
}

- (void) setPicture:(UIImage *)image {
    self.profilePic = image;
    [self.pictureButton setBackgroundImage:image forState:UIControlStateNormal];
}

@end

当在主 UIViewController 上调用 pickImage 时,它将 UIImagePicker 中的图像存储到 [=20= 上的 profilePic 属性 ] 并使用原始 setPicture 方法分配图像。