从服务器加载图像

时间:2017-11-29 11:27:30

标签: android image imageview

这是从android中的服务器获取图像的最快和最好的方法             应用程序。

export class ScheduleComponent implements OnInit {
  @ViewChild("f") scheduleForm: NgForm;
  dateRdioboxSelection;
  loading = true;
  timeArray = [];
  responseData;
  uniqueDate;
  user;
  returnList;
  slots;
  returns;
  selectedTime: string;
  selectedDate: string;
  confirmation: false;
  users = [];
  timeOptions = [];
  dateOptions = [];
  pinId;
  hide = true;
  credential = {};
  constructor(
    private toastr: ToastsManager,
    vRef: ViewContainerRef,
    private router: Router,
    private route: ActivatedRoute,
    private dataService: DataService
  ) {
    console.log("here");
    this.user = JSON.parse(localStorage.getItem("user"));
    console.log(this.user.uidx);
    this.route.params.subscribe(params => {
      this.pinId = +params["id"];
      console.log(this.pinId);
    });
    this.dataService.getUsers(this.user).subscribe(
      data => {
        this.returns = data;
        console.log(JSON.stringify(this.returns));
        this.returnList = data.dbReturnsList;
        this.slots = data.dbSlots;
        console.log(this.returnList, this.slots);
        this.slots.forEach(element => {
          console.log("starttime" + element.slotStartTime);
          console.log("endtime" + element.slotEndTime);
          var startDateandtime = new Date(element.slotStartTime + "z");
          console.log(startDateandtime);
          var startdate = startDateandtime.toDateString();
          var starttime = startDateandtime.getHours();
          var endDateTime = element.slotEndTime.getHours;
          var mystarttime = startdate;
          let myStartTime = this.formatAMPM(element.slotStartTime);
          let myEndTime = this.formatAMPM(element.slotEndTime);
          console.log(myStartTime, myEndTime);
          var mymyStartTime = myStartTime;
          var mymyEndTime = myEndTime;
          console.log(this.timeArray);
          this.timeArray.push(myStartTime);
          // for(let i=0;i<this.timeArray.length; i++) {
          // console.log(this.timeArray[i] + '-');
          // this.timeArray[i] = this.timeArray[i] + "-" + myEndTime;
          // }
          // console.log(this.timeArray);

          //   this.timeOptions = this.timeArray.filter(function(elem, index, self) {
          //     return index === self.indexOf(elem);
          //   });
          //   console.log(this.timeOptions);
          // this.makeTimeArray(mymyStartTime, mymyEndTime);
          // this.dateOptions = startdate;
          console.log(starttime, starttime);
          var options = {
            weekday: "long",
            year: "numeric",
            month: "long",
            day: "numeric"
          };
          var today = new Date(element.slotStartTime + "z");
          console.log(today.toLocaleDateString("en-US"));
          console.log(today.toLocaleDateString("en-US", options));
          console.log(today.toLocaleDateString("hi-IN", options));
          startdate = today.toLocaleDateString("en-US", options);
          this.dateOptions.push(startdate);
          this.uniqueDate = this.dateOptions.filter(function(
            elem,
            index,
            self
          ) {
            return index === self.indexOf(elem);
          });
          console.log(this.uniqueDate);
          this.loading = false;
          //    this.timeOptions.push(starttime);
          //  this.timeOptions = element.timeOptions;
          //  this.dateOptions = element.dateOptions;
          //  console.log(this.timeOptions, this.dateOptions);
        });
      },
      error => {
        console.log("error");
      }
    );
  }
  // arrayOfTime;
  //   makeTimeArray(starttime, endtime) {
  // console.log(starttime, endtime);
  // console.log(this.arrayOfTime);
  // this.arrayOfTime.push(starttime, endtime);
  // for ( let i = 0; i < this.arrayOfTime.length; i++) {
  // this.arrayOfTime = this.arrayOfTime + "-";
  // }
  //  console.log(this.arrayOfTime);
  //   }

  formatAMPM(date) {
    date = new Date(date);
    console.log(date);
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? "pm" : "am";
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? "0" + minutes : minutes;
    var strTime = hours + ":" + minutes + " " + ampm;
    console.log(strTime);
    return strTime;
  }

  onSubmit(form: NgForm) {
    console.log(form.value);
    this.dataService.passValue(form.value);
    console.log(this.slots);
    this.slots.forEach(element => {
      console.log("starttime" + element.slotStartTime);
      console.log("endtime" + element.slotEndTime);
      var startDateandtime = new Date(element.slotStartTime + "z");
      console.log(startDateandtime);
      var startdate = startDateandtime.toDateString();
      var starttime = startDateandtime.getHours();
      var endDateTime = element.slotEndTime.getHours;
      var mystarttime = startdate;
      let myStartTime = this.formatAMPM(element.slotStartTime);
      let myEndTime = this.formatAMPM(element.slotEndTime);
      console.log(myStartTime, myEndTime);
      var mymyStartTime = myStartTime;
      var mymyEndTime = myEndTime;
      console.log(this.timeArray);
      this.timeArray.push(myStartTime);
      var options = {
        weekday: "long",
        year: "numeric",
        month: "long",
        day: "numeric"
      };
      var today = new Date(element.slotStartTime + "z");
      console.log(today.toLocaleDateString("en-US"));
      console.log(today.toLocaleDateString("en-US", options));
      console.log(today.toLocaleDateString("hi-IN", options));
      startdate = today.toLocaleDateString("en-US", options);
      console.log(this.selectedDate, startdate);
      if (this.scheduleForm.value.selectedDate === startdate) {
        console.log("yes");
        console.log(element);
        console.log(element.slotID);
        var slotStartTime = this.formatAMPM(element.slotStartTime);
        var slotEndTIme = this.formatAMPM(element.slotEndTime);
        var start = this.scheduleForm.value.selectedTime.substr(0, 7);
        var end = this.scheduleForm.value.selectedTime.substr(8, 11);
        console.log(start, end, slotStartTime, slotEndTIme);
        if (start === slotStartTime && end === slotEndTIme) {
          console.log(element.slotID);
          var slotIDInSlots = element.slotID;
          console.log(this.returnList);
           var formData = { uidx: "", slotID: "", returnIDs: [] };
          this.returnList.forEach(element => {
            console.log(element);
            console.log(slotIDInSlots);
              var returnId = element.returnID;
              console.log(returnId);
              console.log(this.user);
              formData.uidx = this.user.uidx;
              formData.slotID = slotIDInSlots;
              formData.returnIDs.push(element.returnID);
              console.log(formData);
              this.dataService
                .postPickupData(formData)
                .subscribe(
                  data => {
                    this.responseData = data;
                    console.log(this.responseData);
                    if (this.responseData) {
                      console.log("here");
                      if (
                        this.responseData.status
                          .responseMessage ===
                        "Return already scheduled"
                      ) {
                        this.toastr.info(
                          "Please select another slot!",
                          "Return already scheduled!"
                        );
                      } else {
                        this.router.navigate(["scheduled"]);
                      }
                    } else {
                      this.toastr.error(
                        "Server is unreachable!",
                        "Network Error!"
                      );
                    }
                  },
                  error => {
                    console.log("error");
                  }
                );
            // if (element.slotID === slotIDInSlots) {
            //   var returnId = element.returnID;
            //   console.log(returnId);
            //   console.log(this.user);
            //   var formData = { uidx: "", slotID: "", returnID: "" };
            //   formData.uidx = this.user.uidx;
            //   formData.slotID = slotIDInSlots;
            //   formData.returnID = returnId;
            //   console.log(formData);
            //   this.dataService.postPickupData(formData).subscribe(
            //     data => {
            //       this.responseData = data;
            //       console.log(this.responseData);
            //       if (this.responseData) {
            //         console.log("here");
            //         if (
            //           this.responseData.status.responseMessage ===
            //           "Return already scheduled"
            //         ) {
            //           this.toastr.info(
            //             "Please select another slot!",
            //             "Return already scheduled!"
            //           );
            //         } else {
            //           this.router.navigate(["scheduled"]);
            //         }
            //       } else {
            //         this.toastr.error(
            //           "Server is unreachable!",
            //           "Network Error!"
            //         );
            //       }
            //     },
            //     error => {
            //       console.log("error");
            //     }
            //   );
            // }
          });
       //   console.log("here man");
       //   this.toastr.error("Server is unreachable!", "Network Error!");
        }
      } else {
        console.log("no");
      }
    });
  }

  ngOnInit() {
    console.log(this.selectedDate);
  }



  onSelectionChange() {
    this.timeOptions = [];
    console.log(this.selectedDate);
    console.log(this.slots);
    this.slots.forEach(element => {
      console.log("starttime" + element.slotStartTime);
      console.log("endtime" + element.slotEndTime);
      var startDateandtime = new Date(element.slotStartTime + "z");
      console.log(startDateandtime);
      var startdate = startDateandtime.toDateString();
      var starttime = startDateandtime.getHours();
      var endDateTime = element.slotEndTime.getHours;
      var mystarttime = startdate;
      let myStartTime = this.formatAMPM(element.slotStartTime);
      let myEndTime = this.formatAMPM(element.slotEndTime);
      console.log(myStartTime, myEndTime);
      var mymyStartTime = myStartTime;
      var mymyEndTime = myEndTime;
      console.log(this.timeArray);
      this.timeArray.push(myStartTime);
      var options = {
        weekday: "long",
        year: "numeric",
        month: "long",
        day: "numeric"
      };
      var today = new Date(element.slotStartTime + "z");
      console.log(today.toLocaleDateString("en-US"));
      console.log(today.toLocaleDateString("en-US", options));
      console.log(today.toLocaleDateString("hi-IN", options));
      startdate = today.toLocaleDateString("en-US", options);
      console.log(this.selectedDate, startdate);
      if (this.selectedDate === startdate) {
        console.log("yes");
        console.log(element);
        var slotStartTime = this.formatAMPM(element.slotStartTime);
        var slotEndTIme = this.formatAMPM(element.slotEndTime);
        console.log(slotStartTime, slotEndTIme);
        var res = slotStartTime.concat("-" + slotEndTIme);
        console.log(res);
        this.timeOptions.push(res);
      } else {
        console.log("no");
      }
    });
  }
}

或任何其他格式加载图像非常快,因为在正常的互联网速度。

2 个答案:

答案 0 :(得分:2)

如上所述,here base64格式比普通图像大%37。因此,如果您使用base64格式,您将拥有更大的图像和更长的下载时间。在这种情况下,您需要使用普通图像网址。

要尽快下载图片,您可以使用PicassoGlide

答案 1 :(得分:0)

只需使用滑动可以在应用程序中加快图像加载速度,因为滑动会使用图像缓存,并在压缩图像后加载图像。即使Google向开发人员推荐此库,Google也会在Gmail中使用此库。