美文网首页
转化时间戳、改变图片颜色、项目中数据库sql语句

转化时间戳、改变图片颜色、项目中数据库sql语句

作者: J_HX | 来源:发表于2017-03-31 16:46 被阅读14次

    +(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    
    
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    

    }

    • (UIImage *)imageMaskWithColor:(UIColor *)maskColor {
      if (!maskColor) {
      return nil;
      }
      UIImage *newImage = nil;

      CGRect imageRect = (CGRect){CGPointZero,self.size};
      UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);

      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextScaleCTM(context, 1.0, -1.0);
      CGContextTranslateCTM(context, 0.0, -(imageRect.size.height));

      CGContextClipToMask(context, imageRect, self.CGImage);//选中选区 获取不透明区域路径
      CGContextSetFillColorWithColor(context, maskColor.CGColor);//设置颜色
      CGContextFillRect(context, imageRect);//绘制
      newImage = UIGraphicsGetImageFromCurrentImageContext();//提取图片

      UIGraphicsEndImageContext();
      return newImage;
      }

    /**

    • 打开数据库
      */
    • (BOOL)openDb {
      NSString path = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
      NSString pathName = [path stringByAppendingString:@"LTGreenDB.sqlite"];
      NSLog(@"%@", pathName);
      db = [FMDatabase databaseWithPath:pathName];
      int result = [db open];
      if (result) {
      NSLog(@"打开成功");
      } else {
      NSLog(@"打开失败");
      }
      return result;
      }
      /
    • 创建表格
      */
    • (void)creatShopGreenChildTable {
      if ([self openDb]) {
      BOOL result = [db executeUpdate:@"CREATE TABLE IF NOT EXISTS LTGreenTableView (ltId integer PRIMARY KEY AUTOINCREMENT, dataId integer NOT NULL, typeId integer NOT NULL, tpyeDicID integer NOT NULL, LTDic TEXT NOT NULL);"];
      if (result) {
      NSLog(@"创建表格成功");
      } else {
      NSLog(@"创建表格失败");
      }
      [db close];
      }
      }
      /**
    • 插入了数据
      */
    • (void)insertDataSource:(NSArray *)modelArray WithString:(NSString *)string {

      if ([db open]) {
      int p = 0;
      int q = 0;
      for (int i = 0; i < modelArray.count; i++) {
      NSMutableDictionary *dic = [modelArray[i] mutableCopy];
      NSString dicString = [dic mj_JSONString];
      NSString insertSqlite = nil;
      int typeDicID = 0;
      int forId = 0;
      if ([string isEqualToString:@"3"]) {
      typeDicID = [dic[@"type"] intValue];
      if (typeDicID == 3) {
      forId = p;
      p++;
      } else {
      forId = q;
      q++;
      }
      } else {
      forId = i;
      }
      insertSqlite = [NSString stringWithFormat:@"INSERT INTO LTGreenTableView (dataId, tpyeDicID, typeId, LTDic) VALUES ('%d','%d', '%d', '%@')", forId,typeDicID , [string intValue], dicString];
      BOOL res = [db executeUpdate:insertSqlite];
      if (res) {
      // NSLog(@"插入数据成功");
      } else {
      // NSLog(@"插入数据失败");
      }
      }
      [db close];
      }
      }
      /

    • 删除的全部数据
      */
    • (void)deleteAllData {
      if ([db open]) {
      //清空数据表并将自增字段清零
      NSString *deleteSql = @"DELETE FROM LTGreenTableView where 1=1";
      [db executeUpdate:@"UPDATE sqlite_sequence set seq = 0 where name = 'LTGreenTableView'"];
      BOOL res = [db executeUpdate:deleteSql];
      if (res) {
      NSLog(@"清空shopCar表数据成功");
      } else {
      NSLog(@"清空shopCar表数据失败");
      }

        [db close];
      

      }

    }
    /**

    • 查找方法
      */
    • (NSArray<NSMutableDictionary *> *)selectTypeData:(NSString *)type tpyeDicID:(int)tpyeDicID {
      NSMutableArray *arr = [NSMutableArray array];
      if ([db open]) {
      NSString * sql = [NSString stringWithFormat:@"SELECT * FROM LTGreenTableView WHERE typeId = '%d' and tpyeDicID = '%d'", [type intValue], tpyeDicID];
      FMResultSet * rs = [db executeQuery:sql];
      while ([rs next]) {
      NSString *dic = [rs stringForColumn:@"LTDic"];
            NSData *dat = [dic dataUsingEncoding:NSUTF8StringEncoding];
            NSMutableDictionary *d = [[NSJSONSerialization JSONObjectWithData:dat options:0 error:nil] mutableCopy];
            if ([type isEqualToString:@"3"]) {
                int sd = [rs intForColumn:@"dataId"];
                [d setObject:[NSString stringWithFormat:@"%d", sd] forKey:@"dataId"];
            }
            [arr addObject:d];
        }
        [db close];
    }
    return arr;
    

    }
    /**

    • 修改资料的查询方法
      */
    • (NSMutableDictionary *)selectType:(NSString *)type tpyeDicID:(int)tpyeDicID dataId:(NSString *)dataId {
      NSMutableDictionary *d = [NSMutableDictionary dictionary];
      if ([db open]) {
      NSString * sql = [NSString stringWithFormat:@"SELECT * FROM LTGreenTableView WHERE typeId = '%d' and tpyeDicID = '%d' and dataId = '%d'", [type intValue], tpyeDicID, [dataId intValue]];
      FMResultSet * rs = [db executeQuery:sql];
      while ([rs next]) {
      NSString *dic = [rs stringForColumn:@"LTDic"];
      NSLog(@"%@", dic);
      NSData *dat = [dic dataUsingEncoding:NSUTF8StringEncoding];
      d = [[NSJSONSerialization JSONObjectWithData:dat options:0 error:nil] mutableCopy];
      }
      [db close];
      }
      return d;
      }

    /**

    • 修改数据方法
      */
    • (BOOL)updataDbidData:(NSString *)updata Dictype:(int)Dictype seleDataID:(NSString *)seleDataId btnSelect:(BOOL)btnSelect
      {
      BOOL res = nil;
      NSMutableDictionary *dic = [self selectType:updata tpyeDicID:Dictype dataId:seleDataId];
      if (btnSelect) {
      [dic setObject:@"1" forKey:@"FLAG"];
      NSLog(@"/开%d", btnSelect);
      } else {
      [dic setObject:@"0" forKey:@"FLAG"];
      NSLog(@"、关%d", btnSelect);
      }
      NSString *stringDic = [dic mj_JSONString];
      if ([db open]) {
      NSLog(@" updata === %@, dictype = %d , seleddataid = %d", updata, Dictype, [seleDataId intValue]);
      NSString *updateSql = [NSString stringWithFormat:@"update LTGreenTableView set LTDic = '%@' where typeId = '%d' and tpyeDicID = '%d' and dataId = '%d'", stringDic, [updata intValue], Dictype, [seleDataId intValue]];
      res = [db executeUpdate:updateSql];
      [db close];
      }
      return res;
      }

    相关文章

      网友评论

          本文标题:转化时间戳、改变图片颜色、项目中数据库sql语句

          本文链接:https://www.haomeiwen.com/subject/bnyvottx.html