HGPhWViewController.m 9.4 KB
Newer Older
freecui's avatar
freecui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
//
//  HGPhWViewController.m
//  Cruiser
//  图片预览
//  Created by freecui on 15/7/24.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "HGPhWViewController.h"
#import "HGPhotoWall.h"
#import "HGPhoto.h"
#import "ZYQAssetPickerController.h"
#import "XFDB.h"
#import "XFPhoto.h"

@interface HGPhWViewController ()<HGPhotoWallDelegate,UIActionSheetDelegate,ZYQAssetPickerControllerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate,UIImagePickerControllerDelegate>

@property (strong, nonatomic) UIView *coverV;
@property (strong, nonatomic) NSMutableArray *imgesMuArr;//每次从相册中选择的照片
//@property (strong, nonatomic) NSMutableArray *allImgsMuArr;
@property (strong, nonatomic) IBTUIButton *sureBtn;
@property (strong, nonatomic) UIScrollView *scrollV;

#define kImagCount 3
#define kPadding (20.0 / 320.0 * self.view.width)/(kImagCount + 1)
#define kImagWH ((self.view.width - (kImagCount + 1.0) * kPadding) / kImagCount)

#define KpaddingPhonto  kPadding + kImagWH
@end

@implementation HGPhWViewController
static HGPhWViewController *_instance = nil;

+ (instancetype) shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    
    return _instance;
}

// 重写init方法
- (id)init {
    self = [super init];
    if (!self) {
        return nil;
    }
     //self.photoWall = [[HGPhotoWall alloc]init];
   // [self initHGPhW];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"图片预览";
    [self initHGPhW];
    [self initData];
}
- (void)viewDidDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] postNotificationName:kimgsFiles object:self];
}
- (void)initData {
    self.imgesMuArr = [NSMutableArray array];
//    self.allImgsMuArr = [NSMutableArray array];
//    self.allImgAssets = [NSMutableArray array];
    NSArray *arrData = [[XFDB sharedInstance]fetchAllPhotosFromLocal];
    for (int count = 0; count < arrData.count; count ++) {
        XFPhoto *xfphoto = arrData[count];
        [self addOnePhoto:[UIImage imageWithData:xfphoto.photoData]];
    }
}
- (void)initHGPhW {
    self.edgesForExtendedLayout = NO;
//    self.sureBtn = [IBTUIButton RoundCornerBtnWithTitle:@"确定上传图片" bgColor:nil];
//    _sureBtn.frame = CGRectMake(10, self.view.height - 50 - 64, self.view.width - 20, 40);
//    [_sureBtn addTarget:self action:@selector(surePhoto) forControlEvents:UIControlEventTouchUpInside];
//    [_sureBtn setBackgroundColor:[UIColor redColor]];
//    [self.view addSubview:_sureBtn];
    
    self.scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    [self.view addSubview:_scrollV];
    
    self.photoWall = [[HGPhotoWall alloc]init];
    _photoWall.backgroundColor = [UIColor yellowColor];
    _photoWall.frame = (CGRect) {
        .origin.x = 0,
        .origin.y = 0,
        .size.width = self.view.width,
        .size.height = self.scrollV.height
    };//self.view.bounds;
    _photoWall.delegate = self;
    
#pragma 初始化原来的图片数据  从数据库中读出
    [_photoWall setPhotos:nil];//
                     //  @"http://www.weiphone.com/images_v3/logo.png",nil]];

    [_photoWall setEditModel:YES];
    //[self.view addSubview:_photoWall];
    _scrollV.contentSize = _photoWall.size;
    [_scrollV addSubview:_photoWall];
    [self scrollViewContentHeight];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark 点击图片并放大
//- (void)photoWallPhotoTaped:(NSUInteger)index
//{
- (void)photoWallPhotoTaped:(HGPhoto*)hgPhoto {
    [hgPhoto showImage];
//    UIImageView *imgV = [[UIImageView alloc]init];
//    imgV.frame =   _coverV.bounds;
//    [imgV setImageWithURL:self.photoWall]
//    UIActionSheet *actionSheetTemp = [[UIActionSheet alloc] initWithTitle:nil
//                                                                 delegate:self
//                                                        cancelButtonTitle:@"取消"
//                                                   destructiveButtonTitle:@"删除图片"
//                                                        otherButtonTitles:nil, nil];
//    [actionSheetTemp showInView:self.view];
  
    
}
- (void)photoWallPhotoDel:(NSUInteger)index {
    [_photoWall deletePhotoByIndex:index];
}

- (void)delCoverV {
    if (_coverV) {
        [_coverV removeFromSuperview];
    }

}
- (void)photoWallMovePhotoFromIndex:(NSInteger)index toIndex:(NSInteger)newIndex
{
    
}

- (void)photoWallAddAction
{
//    [self.photoWall addPhoto:_url];
   // [self.photoWall addPhoto:@"http://cc.cocimg.com/bbs/attachment/upload/92/1226921340986127.jpg"];
    [self.photoWall addPhotoImg:_img];
}

- (void)photoWallAddFinish
{
    
}

- (void)photoWallDeleteFinish
{
    
}



#pragma 增加一张照片
- (void)addOnePhoto: (UIImage *)img {
  // [self.photoWall addPhoto:@"http://cc.cocimg.com/bbs/attachment/upload/92/1226921340986127.jpg"];
    self.img = img;
    [self.photoWall addPhotoImg:img];
    NSLog(@"%@",NSStringFromCGRect(_photoWall.frame));
    
}
#pragma 增加多张照片
- (void)addPhotosWithArrayImgs:(NSArray *)arr {
    for(UIImage *img in arr){
        [self addOnePhoto:img];
    }

}

#pragma 照片选择
- (void)photowallChoosePhontoes {
   UIActionSheet *actionSheet =  [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开照相机", @"从手机相册获取", nil];
    [actionSheet showInView:self.view];

}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [self openCamera];
            break;
        case 1:
            [self libraryPhoto];
            break;
        default:
            break;
    }

}
- (void)openCamera {
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = sourceType;
    [self PresentModalViewController:picker animated:YES];
    
}
- (void)libraryPhoto {

    ZYQAssetPickerController *assetPicker = [[ZYQAssetPickerController alloc]init];
    assetPicker.maximumNumberOfSelection = 10;
    assetPicker.assetsFilter = [ALAssetsFilter allPhotos];
    assetPicker.showEmptyGroups = NO;
    assetPicker.delegate = self;
   // assetPicker.indexPathsForSelectedItems = _allImgAssets;
    assetPicker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration >= 5;
        } else {
            return YES;
        }
    }];
    
    [self PresentModalViewController:assetPicker animated:YES];
    
}

#pragma UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self addOnePhoto:img];
    NSData *imgData = UIImagePNGRepresentation(img);
    [self savePhotoToDB:imgData];
    
    
 //   [self.allImgsMuArr addObject:imgData];
    [self scrollViewContentHeight];
    [picker DismissModalViewControllerAnimated:YES];
}

#pragma 把选择的图片放入到本地数据库中
- (void)savePhotoToDB: (NSData *)imgData {
    XFPhoto *xfphoto = [[XFPhoto alloc]initWithPhotoData:imgData];
    BOOL isSave = [[XFDB sharedInstance]savePhoto:xfphoto];
    if (isSave) {
        NSLog(@"保存成功");
    } else {
        NSLog(@"保存失败");
    }
}
#pragma mark - ZYQAssetPickerController Delegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{

    
    if (_imgesMuArr.count > 0) {
        [_imgesMuArr removeAllObjects];
    }
    
   // dispatch_async(dispatch_get_global_queue(0, 0), ^{
        for (int i=0; i<assets.count; i++) {
            ALAsset *asset = assets[i];
            CGImageRef ref = [[asset  defaultRepresentation]fullResolutionImage];
            UIImage *img = [[UIImage alloc]initWithCGImage:ref];
            NSData *imgData = UIImagePNGRepresentation(img);
            [self savePhotoToDB:imgData];
            // UIImage *tempImg = [UIImage imageWithCGImage:[asset po]];
            [self.imgesMuArr addObject: img];//[UIImage imageNamed:@"AttachCamera"]
            //   [self.allImgsMuArr addObject:imgData];
        }
        [self addPhotosWithArrayImgs:_imgesMuArr];
  //  });
    [self scrollViewContentHeight];
    [picker popoverPresentationController];

}

#pragma 调整scrollView内容视图内容高度
- (void)scrollViewContentHeight {
    NSArray *arr = [[XFDB sharedInstance] fetchAllPhotosFromLocal];
    NSInteger count = 1;
    if ((arr.count + 1) % kImagCount) {
        count += (arr.count + 1) / kImagCount + 1;
    } else {
        count  += (arr.count + 1) / kImagCount ;
    }
    self.photoWall.height = 15.0 / 568 * self.view.height + KpaddingPhonto * count;
    self.scrollV.contentSize = _photoWall.size;

}
#pragma 确定选择的照片
- (void)surePhoto {
   
}
@end