1

我的项目中有一个视图控制器,我需要在其中单击两个图像并在同一屏幕上以缩放形式显示它们。将两个图像上传到服务器后,可以重复此过程。为了拍照,我使用 UIImagePickerController。点击图片 8 次后,我收到了 1 级和 2 级的内存警告。

当我在 StackOverflow 上提到相关问题时,他们中的大多数人建议将 UIImagePickerController 设为 Singleton 对象。所以我创建了一个新的 Singleton 类,它是 UIImagePickerController 的子类。现在,在我的主视图控制器中,我正在创建这个自定义类的对象并重复使用它。但不幸的是,这也无济于事。点击 8 次图片后,我仍然收到内存警告。

还发布了一些片段-

创建 UIImagePickerController 的子类 --

@interface ImagePickerControllerSingelton : UIImagePickerController <UIImagePickerControllerDelegate>
{

}
+(ImagePickerControllerSingelton *) sharedPicker;

单例类的实现——

static ImagePickerControllerSingelton *sharedInstance = nil;

+(ImagePickerControllerSingelton*) sharedPicker
{
   @synchronized (self){
       if (sharedInstance == nil) {
           [[self alloc] init];
           return sharedInstance;
       }
   }
   return sharedInstance;
}

+ (id)allocWithZone:(NSZone *)zone
{
   @synchronized(self) {
       if (sharedInstance == nil) {
           sharedInstance = [super allocWithZone:zone];
           return sharedInstance;  // assignment and return on first allocation

       }
   }

   return nil; //on subsequent allocation attempts return nil
}

现在在主视图控制器类中——

ImagePickerControllerSingelton *imagePicker;

imagePicker = [ImagePickerControllerSingelton sharedPicker];
// Set source to the camera
imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

// Delegate is self
imagePicker.delegate = self;

// Allow editing of image?
imagePicker.allowsEditing = YES;
imagePicker.showsCameraControls = NO;
imagePicker.wantsFullScreenLayout = YES; 

imagePicker.cameraOverlayView = [self createOverlayView]; 
[self presentModalViewController:imagePicker animated:YES];

难道我做错了什么?请各位大侠帮忙!

4

0 回答 0