【问题标题】:UIImage gets released at the end the function after assignment has been madeUIImage 在完成分配后的函数结束时被释放
【发布时间】:2015-03-11 11:22:05
【问题描述】:

我有一个 ViewController,它拥有一个自定义 UIView 作为它的成员,如下所示:@property (nonatomic, strong) IBOutlet ProfileUIView * profileUIView;
在我的 ViewController 中,我从 imagePickerController:didFinishPickingMediaWithInfo: 那里得到一个 UIImage '我将 UIImage 分配给我的 ProfileUIView 中的本地 UIImage 变量。我看到 UIImage 变量被分配,但是,在 set 函数结束后 UIImage 变量得到 nil 并且我以某种方式丢失了图像。

这是 imagePickerController:didFinishPickingMediaWithInfo: 的 sn-p: 在我的 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];


我该如何解决?
谢谢!

【问题讨论】:

    标签: ios objective-c uiimage assign


    【解决方案1】:

    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 的图像存储到UIViewControllerprofilePic 属性中,并使用原始setPicture 方法分配图像。

    【讨论】:

    • 我刚刚尝试了您的解决方案。它和以前一样工作。 self.profilePic 将 UIImage 保存在控制器内,但嵌套视图在退出 setPicture 后仍然会丢失它。
    • 你是如何在ProfileUIView里面声明属性profilePic的?
    • @property (nonatomic, 保留) UIImage * profilePic;
    • 您使用的是 ARC,对吗?我正在设置它以重现它。
    • 据我所知,我知道。我对目标 c 有点陌生。顺便说一句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2013-05-24
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多